好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

用Python将CSV数据加载到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"

如果有人能看一眼,我将不胜感激.

谢谢.

我想你必须把mydb测试数据mit()全部插入到.

像这样的东西

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测试数据mit()
cursor.close()
print "Done"

查看更多关于用Python将CSV数据加载到MySQL中的详细内容...

  阅读:17次