简体   繁体   English

Python SQLite在查询中使用变量的最佳方法

[英]Python SQLite best way to use variables in query

Why do i get this error? 为什么会出现此错误?

sqlite3.OperationalError: near "?": syntax error

when i run this: 当我运行这个:

c.execute('UPDATE ? SET Quantity = Quantity + ? WHERE Date = ?', (table, amount, date))

But not when i run this? 但是当我运行这个时不是吗?

c.execute('UPDATE table1 SET Quantity = Quantity + ? WHERE Date = ?', (amount, date))

Variable value is: 变量值为:

table = 'table1'
amount = 20
Date = '12/5/2014'

I'm trying to dynamically create tables, but just doesn't work out. 我正在尝试动态创建表,但无法解决问题。

You can't use placeholders for table names. 您不能将占位符用于表名。 You have to use normal Python string formatting or concatenation. 您必须使用普通的Python字符串格式或串联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM