用Python将CSV数据加载到MySQL中

用Python将CSV数据加载到MySQL中,第1张

概述不知道我在这里缺少什么,但这段代码运行没有任何错误信息,但表中没有任何内容.我正在将三列中的CSV值加载到 mysql表中 import csvimport MySQLdbmydb = MySQLdb.connect(host='localhost', user='root', passwd='', db='mydb')cursor = mydb.cursor() 不知道我在这里缺少什么,但这段代码运行没有任何错误信息,但表中没有任何内容.我正在将三列中的CSV值加载到 mysql表中

import csvimport MysqLdbmydb = 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 csvimport MysqLdbmydb = MysqLdb.connect(host='localhost',row)#close the connection to the database.mydb.commit()cursor.close()print "Done"
总结

以上是内存溢出为你收集整理的用Python将CSV数据加载到MySQL中全部内容,希望文章能够帮你解决用Python将CSV数据加载到MySQL中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1191078.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存