简体   繁体   中英

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. 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 :

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

or if you're using Python 3.6+, you can use f-strings:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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