不知道我在这里缺少什么,但这段代码运行没有任何错误信息,但表中没有任何内容.我正在将三列中的CSV值加载到 mysql表中 import csvimport MySQLdbmydb = MySQLdb.connect(host='localhost', user='root', pa
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.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"