我一直在搜索 Stack Overflow 以及其他一些在线研究,寻找将变量插入我的 SQLite 查询的正确语法。但是,这 3 种支持的语法都不适用于我的查询。
我发现 SQLite DB-API 支持的三种语法是:
qmark:
curs.execute("SELECT * FROM actors where actor.first_name = ?",("D'Angelo", ))
numeric:
curs.execute("SELECT * FROM actors where actor.first_name = :1", ("D'Angelo", ))
named:
curs.execute("SELECT * FROM actors where actor.first_name = :first_name",{'first_name': "D'Angelo"})
取自http://www.philvarner.com/test/ng-python3-db-api/
但是,当我执行以下操作时,我收到这些错误:
named:
tableListQuery = "SELECT name FROM :dbFile WHERE type='table' ORDER BY Name", {'dbFile': dbFile}
userCursor.execute(tableListQuery)
Pycharm error: Expected type 'str', got 'Tuple[str, Dict[str, Any]]'
Terminal error:
File "./dedupeDatabase.py", line 15, in Describe
userCursor.execute(tableListQuery)
ValueError: operation parameter must be str
-------------------------------------------------
qmark:
tableListQuery = "SELECT name FROM ? WHERE type='table' ORDER BY Name", (dbFile, )
userCursor.execute(tableListQuery)
Pycharm error: Expected type 'str', got 'Tuple[str, Tuple[Any]]' instead
Terminal error:
File "./dedupeDatabase.py", line 15, in Describe
userCursor.execute(tableListQuery)
ValueError: operation parameter must be str
-------------------------------------------------
numeric:
tableListQuery = "SELECT name FROM :1 WHERE type='table' ORDER BY Name", (dbFile, )
userCursor.execute(tableListQuery)
Pycharm error: Expected type 'str', got 'Tuple[str, Tuple[Any]]'
Terminal error:
File "./dedupeDatabase.py", line 15, in Describe
userCursor.execute(tableListQuery)
ValueError: operation parameter must be str
请提供您对此问题的任何见解。