不知道我在这里缺少什么,但这段代码运行没有任何错误信息,但表中没有任何内容.我正在将三列中的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中的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did171193