麻烦迭代地将dict插入sqlite3数据库

时间:2014-04-20 11:29:12

标签: python sqlite

我疯了。为什么这不起作用?

con = lite.connect('test.db')

with con:
    cur = con.cursor()    
    for key in merged_dict:
        print str(key) + ": " + str(merged_dict[key])
        cur.execute("INSERT OR REPLACE INTO Games (Url, '" + str(key) + "'), VALUES ('" +merged_dict['Url'] + "', '" + str(merged_dict[key]) + "')")

给出了这个错误:

Traceback (most recent call last):
  File "/home/benjamin/Documents/insertTest.py", line 7, in <module>
    cur.execute("INSERT OR REPLACE INTO Games (Url, '" + str(key) + "'), VALUES ('" +merged_dict['Url'] + "', '" + str(merged_dict[key]) + "')")
OperationalError: near ",": syntax error

我也尝试打印出查询而不是执行它们。这是一个示例:

INSERT OR REPLACE INTO Games (Url, 'h1_home'), VALUES ('ILP9hhls', '0')

这是非法的查询吗?

1 个答案:

答案 0 :(得分:1)

在VALUES之前不应该有逗号,即:

INSERT OR REPLACE INTO Games (Url, 'h1_home') VALUES ('ILP9hhls', '0')

以下是供参考的示例查询:

http://www.w3schools.com/sql/sql_insert.asp