[英]Sqlite3 'executemany' with UPDATE and INSERT using python dictionary keys not acting the same?
I have two sqlite queries that use executemany to insert and update data sets from a python dictionary.我有两个 sqlite 查询,它们使用 executemany 从 python 字典插入和更新数据集。 With the insert
query the dictionary.values()
as the executemany second argument works perfectly, but with the update I do not get anything updated.通过insert
查询, dictionary.values()
作为 executemany 第二个参数工作得很好,但是通过更新我没有得到任何更新。 Can someone please shed some light on why there is a differrence?有人可以解释一下为什么会有差异吗?
Insert code sample:插入代码示例:
query = "INSERT OR REPLACE INTO farm_tb(farm_id, farm)\
VALUES(:frmid, :frm)"
cur.executemany(query, arr.values())
conn.commit()
Update sample:更新示例:
query = ("UPDATE emp_log_tb SET syncstatus=syncstatus WHERE log_id=log_id", (':syncstatus', ':logid'))
cur.executemany(query, arr.values())
conn.commit()
Both data sets looks similar:两个数据集看起来很相似:
insert data:插入数据:
{'0': {'farmid': '4', 'farm': 'farm1'}, '1': {'farmid': '3', 'farm': 'farm6'}}
update data:更新数据:
{'0': {'logid': '1', 'syncstatus': 1}, '1': {'logid': '2', 'syncstatus': 1}, '2': {'logid': '3', 'syncstatus': 1}, '3': {'logid': '4', 'syncstatus': 1}, '4': {'logid': '5', 'syncstatus': 1}}
That update statement just sets every row's syncstatus
to its current value, because log_id = log_id
is always true (Unless it's a NULL
, of course).该更新语句只是将每一行的syncstatus
设置为其当前值,因为log_id = log_id
始终为真(当然,除非它是NULL
)。 Plus you're assigning a tuple to query
, not a string.另外,您正在为query
分配一个元组,而不是字符串。 Not sure what executemany()
does with that.不确定executemany()
做了什么。
You want something like你想要类似的东西
cur.executemany("UPDATE emp_log_tb SET syncstatus=:syncstatus WHERE log_id=:log_id", arr.values())
Note the named parameters in the UPDATE
like the ones in the INSERT
.注意UPDATE
的命名参数,就像INSERT
。
"UPDATE emp_log_tb SET syncstatus=syncstatus WHERE log_id=log_id", (':syncstatus', ':logid')
应该是UPDATE emp_log_tb SET syncstatus=:syncstatus WHERE log_id=:log_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.