【发布时间】:2016-01-25 00:48:39
【问题描述】:
我正在尝试从字典中获取数据(该示例为便于阅读而进行了简化)并将其插入到 mysql 数据库中。 我有以下代码。
import pymysql
conn = pymysql.connect(server, user , password, "db")
cur = conn.cursor()
ORFs={'E7': '562', 'E6': '83', 'E1': '865', 'E2': '2756 '}
table="genome"
cols = ORFs.keys()
vals = ORFs.values()
sql = "INSERT INTO %s (%s) VALUES(%s)" % (
table, ",".join(cols), ",".join(vals))
print sql
print ORFs.values()
cur.execute(sql, ORFs.values())
cur.close()
conn.close()
print sql 语句返回INSERT INTO genome (E7,E6,E1,E2) VALUES(562,83,865,2756 )
当我直接在 mysql 命令行中输入它时, mysql 命令有效。但是当我运行 python 脚本时出现错误:
<type 'exceptions.TypeError'>: not all arguments converted during string formatting
args = ('not all arguments converted during string formatting',)
message = 'not all arguments converted during string formatting'
一如既往,我们将不胜感激任何建议。
【问题讨论】:
标签: python mysql dictionary pymysql