开发者社区> 问答> 正文

在Python中将CSV数据加载到MySQL中?mysql

不知道我在这里缺少什么,但是这段代码运行时没有任何错误消息,但是表中没有任何内容。我正在将三列的CSV值加载到mysql表中

import csv import MySQLdb

mydb = MySQLdb.connect(host='localhost', user='root', passwd='', db='mydb') cursor = mydb.cursor()

csv_data = csv.reader(file('students.csv')) for row in csv_data:

cursor.execute('INSERT INTO testcsv(names, \
      classes, mark )' \
      'VALUES("%s", "%s", "%s")', 
      row)

#close the connection to the database. cursor.close() print "Done" 如果有人可以看看,将不胜感激。

谢谢。

展开
收起
保持可爱mmm 2020-05-17 21:13:03 1287 0
1 条回答
写回答
取消 提交回答
  • 我认为您必须将mydb.commit()所有内容插入。

    像这样

    import csv import MySQLdb

    mydb = MySQLdb.connect(host='localhost', user='root', passwd='', db='mydb') cursor = mydb.cursor()

    csv_data = csv.reader(file('students.csv')) for row in csv_data:

    cursor.execute('INSERT INTO testcsv(names, \
          classes, mark )' \
          'VALUES("%s", "%s", "%s")', 
          row)
    

    #close the connection to the database. mydb.commit() cursor.close() print "Done"来源:stack overflow

    2020-05-17 21:25:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载