[英]Python sqlite3 update multiple rows with WHERE statement
I'm trying to update multiple rows in my database with this code:我正在尝试使用以下代码更新数据库中的多行:
conn = sqlite3.connect("usersEsteso.db")
c = conn.cursor()
record_id = name
# query the database
c.execute(f"""UPDATE usersEsteso SET
f_name = :first,
P_iva = :P_iva,
cell_number = :cell_number,
tax = :tax,
inps = :inps
WHERE f_name = f'{record_id}'""",
{
"first": f_name_entry.get(),
"P_iva": P_iva_entry.get(),
"cell_number": cell_number_entry.get(),
"tax": tax_entry.get(),
"inps": inps_entry.get(),
}
)
conn.commit()
conn.close()
but the result is:但结果是:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "D:\Willow\WilloW SW Gestionale\schermata utenti 2.py", line 37, in save_edit
c.execute(f"""UPDATE usersEsteso SET
sqlite3.OperationalError: near "'sam'": syntax error
'sam' is the string inside the variable name 'sam' 是变量名中的字符串
If I try to insert manually the string 'sam' inside the WHERE statement, the code works如果我尝试在 WHERE 语句中手动插入字符串“sam”,则代码有效
solved:解决了:
I used a placeholder in WHERE statement and assigned it in the dictionary below:我在 WHERE 语句中使用了一个占位符,并在下面的字典中分配了它:
conn = sqlite3.connect("usersEsteso.db")
c = conn.cursor()
record_id = name
c.execute(f"""UPDATE usersEsteso SET
f_name = :first,
P_iva = :P_iva,
cell_number = :cell_number,
tax = :tax,
inps = :inps
WHERE f_name = :record""",
{
"first": f_name_entry.get(),
"P_iva": P_iva_entry.get(),
"cell_number": cell_number_entry.get(),
"tax": tax_entry.get(),
"inps": inps_entry.get(),
"record": record_id
}
)
conn.commit()
conn.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.