简体   繁体   English

传递SQL列名称作为cursor.execute()的属性

[英]Pass SQL column name as attribute of cursor.execute()

I want to pass a column name as attribute of the function cursor.execute() with 我想将列名作为函数cursor.execute()属性传递给

conn = sqlite3.connect(db)
cursor = conn.cursor()
cursor.execute("INSERT INTO table (?) VALUES (?)", (column, value))

Is that possible? 那可能吗?

In the current form I receive the error SyntaxError: invalid syntax 在当前形式下,我收到错误SyntaxError: invalid syntax

This will work, assuming column is not user defined input. 假设column不是用户定义的输入,这将起作用。

cursor.execute(
    "INSERT INTO table ({column}) VALUES (?)".format(column=column),
    (value,)
)

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

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