Python Sqlite认为我缺少绑定

Python Sqlite认为我缺少绑定,python,sqlite,Python,Sqlite,由于缺乏关于如何从字典中用Python创建sqlite查询的任何源代码,我构建了自己的: updates = ', '.join(["`"+field+"`" + '=:'+field for field in information.keys() if field != 'name']) where = ' WHERE name == :name' values = {':'+field: value for field, value in information.it

由于缺乏关于如何从字典中用Python创建
sqlite
查询的任何源代码,我构建了自己的:

    updates = ', '.join(["`"+field+"`" + '=:'+field for field in information.keys() if field != 'name'])
    where = ' WHERE name == :name'
    values = {':'+field: value for field, value in information.items()}

    query = 'UPDATE firms SET ' + updates + where
    c.execute(query, values)
然而,我明白了

sqlite3.ProgrammingError: You did not supply a value for binding 1.
这让我困惑,因为我认为我已经提供了我应该拥有的一切:

In[374]: query
Out[374]: 'UPDATE firms SET `founded`=:founded, `size`=:size, `headquarters`=:headquarters, `type`=:type, `revenue`=:revenue WHERE name == :name'
In[375]: information
Out[375]: 
{'founded': '1962',
 'headquarters': 'Bentonville, AR',
 'name': 'Walmart',
 'revenue': '$10+ billion (USD) per year',
 'size': '10000+ employees',
 'type': 'Company - Public (WMT)'}

键中不需要
。试试这个:

values = {field: value for field, value in information.items()}
或者更简洁地说:

values = information
示例程序:

import sqlite3

conn = sqlite3.connect(":memory:")
c = conn.cursor()

c.execute("create table firms (founded, hq, name, rev, size, type)")
c.execute("insert into firms ( name ) values (?) ",("bar", ))
conn.commit()

def update(information):
    updates = ', '.join(["`"+field+"`" + '=:'+field for field in information.keys() if field != 'name'])
    where = ' WHERE name == :name'
    values = information
    query = 'UPDATE firms SET ' + updates + where
    c.execute(query, values)
    conn.commit()

update(dict(name='bar', founded='1062', rev='1 MILLION DOLLARS!'))
print c.execute('select * from firms').fetchall()
结果:

[(u'1062', None, u'bar', u'1 MILLION DOLLARS!', None, None)]

那些反勾号不应该是双引号吗?@holdenweb我对sqlite不太熟悉,但我从其他地方回忆起,反勾号用于字段,双引号用于指示字符串值。我确实知道,在所有已知版本的SQL中,字符串都是由单引号分隔的。在我熟悉的所有字段中,字段名(引用时)需要双引号。也许这就是错误所在。在查询中,您不需要对列名进行引号,因为它们都是有效的SQL标识符搁置:
updates
赋值的可读性可能更高:
updates=','.join(“{0}=:{0}”。如果字段!=“name”,则信息中字段的格式(字段)
引用规则在这里表示:反勾号是非标准的,但是为了MySql的兼容性而包括。