我已经成功通过以下用户输入动态查询了strings
:
def read_part_of_database(table_column, user_query):
c_lime.execute("SELECT * FROM lime_database_table WHERE {} LIKE '%{}%'"
"ORDER BY time_start".format(table_column, user_query))
for row in c_lime.fetchall():
print(row)
这使用户可以写入string
query
的全部或部分,并以user_query
的形式传递。 LIKE '%{}'
可以接受query
的部分内容。
但是,当user_query
作为integer
而不是string
传递时,database
不返回任何内容。无论是integer
的全部还是部分通过,都是如此。我假设这与string
周围的integers
引号有关。因此,我尝试使用%{}%
而不是'%{}%'
,但这产生了以下错误:
c_lime.execute("SELECT * FROM lime_database_table WHERE {} LIKE {}% ORDER BY time_start".format(table_column, user_query))
sqlite3.OperationalError: near "ORDER": syntax error
所以,我的问题是,如果user_query
是数字而不是string
,我该如何使用query
LIKE
?
作为旁注,我已经成功地使用queried
作为user_query
的{{1}} int
作为= ?
。像这样:
c_lime.execute("SELECT * FROM lime_database_table WHERE {} = ? ORDER BY"
"time_start".format(table_column), [user_query])
但是在这种情况下,用户必须写下整个数字。