Python 如何将包含多个字典的表插入MySQL表中?
我的python代码得出以下结果:Python 如何将包含多个字典的表插入MySQL表中?,python,mysql,Python,Mysql,我的python代码得出以下结果: [ {filename:'1,2',Name:'Gorge',registration number: '6657', registration date: '2012-09-10 14:31:13'}, {filename:'5,43',Name:'mazu',registration number:'45', registration date:'2012-10-08 17:28:47'}] 一旦我想把它放到MySQL表中,我就得到了这个错误:
[
{filename:'1,2',Name:'Gorge',registration number: '6657', registration date: '2012-09-10 14:31:13'},
{filename:'5,43',Name:'mazu',registration number:'45', registration date:'2012-10-08 17:28:47'}]
一旦我想把它放到MySQL表中,我就得到了这个错误:
Traceback (most recent call last):
File "C:\Users\jio\Datasets 1\MyTable_info.py", line 63, in <module>
cur.executemany(query,records)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 243, in executemany
self.errorhandler(self, ProgrammingError, msg.args[0])
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: not enough arguments for format string
是否有人知道我为什么会出现此错误以及错误的含义?尝试引用字段名。记录的内容是什么?不要在字段名中使用空格
如果需要,请用`字符引述它们。您正试图在INT(50)字段中插入字符串 查看表的最后一个字段,注册时间字段。它是一个整数,您试图插入诸如“2012-10-08 17:28:47”或“2012-09-10 14:31:13”之类的值 对于快速修复,只需将注册时间字段类型更改为VARCHAR(50) 但是,对于性能问题,您可能应该考虑使用某种时间戳字段而不是VARCHAR来实现这种目的
除此之外,不要对要添加某种字符串的字段使用INT-type 从中修改records变量 [{文件名:'1,2',姓名:'Gorge',注册号:'6657',注册日期:'2012-09-1014:31:13'}, {文件名:'5,43',姓名:'mazu',注册号:'45',注册日期:'2012-10-08 17:28:47'] 到 [(‘1,2’、‘峡谷’、‘6657’、‘2012-09-1014:31:13’、‘5,43’、‘妈祖’、‘45’、‘2012-10-0817:28:47’)]
并且避免在列名中使用空格记录内容就是我上面粘贴的结果。每个文件都有一个。我正在浏览一个目录。把它贴在这里。99%的情况下,这是因为您在字段名中使用了空格,所以不要这样做。我不能这样做,因为我正在从文件中提取字典的键,而那里的键是这样的
con = MySQLdb.connect(host = "******", port=***, user = "***", passwd="*****", db="****")
with con:
cur=con.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS info(id INT(10) auto_increment primary key,file_name VARCHAR(10),
Name VARCHAR(50),Registration ID INT(50),registration time INT(50))''')
query= "INSERT INTO info (file_name, Name, Registration ID, registration time) VALUES ( %s, %s, %s, %s )"
cur.executemany(query,records)
con.commit()