简体   繁体   English

如何在 python sqlite3 命令中将变量作为列名和表名传递?

[英]How do I pass variables as column and table names in a python sqlite3 command?

I am trying to pass a table name and a column name into a sqlite command in python.我正在尝试将表名和列名传递给 python 中的 sqlite 命令。 From what I understand, this can't be done through traditional methods.据我了解,这是无法通过传统方法完成的。

conn.execute('ALTER TABLE ? ADD COLUMN ?;', t)

I've tried我试过了

{}.format

":X",(X: t)

and a few others, any assistance would be greatly appreciated和其他一些人,任何帮助将不胜感激

Use str.format :使用str.format

conn.execute('ALTER TABLE {} ADD COLUMN {};'.format(table, column))

or if you're using Python 3.6+, you can use f-strings:或者,如果您使用的是 Python 3.6+,则可以使用 f-strings:

conn.execute(f'ALTER TABLE {table} ADD COLUMN {column};')

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

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