How do I get around not being able to parse a table name into a python sqlite query?

别等时光非礼了梦想. 提交于 2020-01-30 08:30:27

问题


I have a python3 program that I'm making which uses a sqlite database with several tables, I want to create a selector module to allow me to chose which table to pull data from.

I have found out that I can't use parameter substitution for a table name as shown bellow, so I'm looking for some alternative methods to accomplish this.

c.execute("SELECT * FROM ? ", DB)

Any ideas?


回答1:


Right. You can not use parameter substitution to specify the table. So instead you must do string manipulation:

c.execute("SELECT * FROM {t} ".format(t=tablename))



回答2:


I don't know if this is a python3 thing but it seems easiest to just do this:

c.execute("SELECT * FROM %s "% tablename)



回答3:


Blockquote * Right. You can not use parameter substitution to specify the table. So instead you must do string manipulation: c.execute("SELECT * FROM {t} ".format(t=tablename))* Blockquote

Thanks unutbu, this is just what I needed.



来源:https://stackoverflow.com/questions/4044512/how-do-i-get-around-not-being-able-to-parse-a-table-name-into-a-python-sqlite-qu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!