[英]SQLite3 Python update whole row with a list
I have table with the following structure: 我有以下结构的表:
c.execute("CREATE TABLE foo (id INTEGER, number INTEGER, number2 REAL, letters TEXT)")
I got the following list 我得到以下清单
list_foo = [9, 1, 2.3, 'abc']
edit: In the table I already have fields, so the table should in this case be updated where id=9 编辑:在表中我已经有字段,因此在这种情况下应该更新表,其中id = 9
How do I update a row with my list variable? 如何使用列表变量更新行?
c.execute("UPDATE foo SET VALUES = ? WERE id = ?", (list_foo, listfoo[0]))
But of course VALUES doesn't work. 但是,当然VALUES不起作用。 How do I instantly update the row without typing: 我如何立即更新行而不输入:
c.execute("UPDATE foo SET id = ?, number = ?, number2 = ?, letters = ? WERE id = ?", (list_foo[1], list_foo[2], list_foo[3], list[0]))
My actual table has more entries than this, so it would be a pain to type it like this. 我的实际表中的条目比这更多,因此像这样键入它会很麻烦。
Edit 2: If it's not possible and I have to use my long SQLite3 code, is it at least possible to have: 编辑2:如果不可能,而我必须使用我的长SQLite3代码,是否至少有:
(list_foo[1:], list_foo[0])
instead of 代替
(list_foo[1], list_foo[2], list_foo[3], list[0]))
Update** (You can hack something by escaping the quotes, other than that I'm not sure if what you are looking for exists) 更新**(您可以通过将引号转义来破解某些内容,但我不确定您所寻找的内容是否存在)
def quote_identifier(s, errors="strict"):
encodable = s.encode("utf-8", errors).decode("utf-8")
nul_index = encodable.find("\x00")
if nul_index >= 0:
error = UnicodeEncodeError("NUL-terminated utf-8", encodable,
nul_index, nul_index + 1, "NUL not allowed")
error_handler = codecs.lookup_error(errors)
replacement, _ = error_handler(error)
encodable = encodable.replace("\x00", replacement)
return "\"" + encodable.replace("\"", "\"\"") + "\""
list_foo = [2, 2, 2.5, 'abd']
tab_names = ('number','number2','letters')
dic = dict(zip(tab_names, list_foo[1:len(list_foo)]))
for x in dic:
c.execute("UPDATE foo SET"+quote_identifier(x)+"=? WHERE rowid=?",[dic[x], list_foo[0]])
rec_con.commit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.