[英]How can I convert SQLite 2 to SQLite3 using Python on Windows?
[英]How can I create a SQLite table using the sqlite3 Python library with parameterized placeholders?
從 Python 的文檔( https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.execute )中,我應該能夠使用參數化占位符執行 SQL 語句。 然而,下面的代碼不起作用。
import sqlite3
conn = sqlite3.connect("temp.db")
c = conn.cursor()
c.execute("create table ? (foo text, bar text)", ("table_name",))
conn.commit()
conn.close()
我收到一個錯誤:
Traceback (most recent call last):
File "main.py", line 6, in <module>
c.execute("create table ? (foo text, bar text)", ("table_name",))
sqlite3.OperationalError: near "?": syntax error
但是如果我不使用參數化占位符,它就可以工作。
一句話——不。 您只能參數化值,不能參數化對象名稱(在本例中為表名稱)。 如果你想做這樣的事情,你必須求助於字符串操作,例如:
c.execute("create table %s (foo text, bar text)" % ("table_name"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.