【发布时间】:2012-08-31 00:13:13
【问题描述】:
我有一个这样的python dict:
my_dict = {'find': ['http://time.com', 'http://find.com'], 'time': ['http://any.com', 'http://www.tim.com', 'http://mine.in']...}
我想在 mysql 表的两个不同列中插入字典 my_dict 的 keys 和 values。列名分别为term 和urls。我现在有三列:id、term、urls 和 我想在不同的行中插入每个键和值对。
我想将链接存储在urls 中,用逗号分隔。 my_dict 值中的网站链接必须以逗号分隔存储,例如http://time.com, http://mine.com.
我尝试按以下方式插入
for i in my_dict.items():
keys = i[0]
values = i[1]
sql = """INSERT INTO index_table (term, urls) VALUES (%s, %s)"""
cursor.execute(sql, (keys, values))
但它显示以下错误:
(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1")
【问题讨论】:
-
您是否检查过您生成的 SQL 是否符合您的预期?
-
为什么你不想使用某种 ORM 工具包?例如
sqlalchemy。
标签: python mysql dictionary insert executemany