将文本文件中的数据存储到mysql选项卡中

2024-04-25 22:55:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文本文件和一个MySQL表。文本文件如下所示。 新建.txt

apple| 3
ball | 4
cat  | 2

像这样。从这个文本文件中,我想在下面的MySQL表中存储数据。在

^{pr2}$

我想在列查询中存储apple、ball、cat,并在count列中存储所有数字3、4、2。默认情况下,in is_prod_ready列为false,in time_of_created将占用当前时间。最后一个“更新”栏将占用更新时间。在

我已经做好桌子了。我无法将文本文件中的所有数据存储到数据库中。我试过下面的代码。在

import MySQLdb
con = MySQLdb.connect(host="localhost",user="root",passwd="9090547207",db="Test")         
cur = con.cursor()
query = 'load data local infile "new.txt" into table data field terminated by "|" lines terminated by "\n" '
cur.execute(query)
con.commit()
con.close()

这里我的数据库名是Test,表名是data。在


Tags: 数据intesttxt数据库appledata时间