如何使用LIKE查询SQL以查找整数? (蟒蛇)

时间:2019-03-01 13:14:13

标签: python sqlite integer

我已经成功通过以下用户输入动态查询了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])

但是在这种情况下,用户必须写下整个数字。

0 个答案:

没有答案