简体   繁体   中英

Python sqlite operation,SQL statement 'where field in (1,2)' syntax error

Python sqlite operation,SQL statement 'where field in (1,2)' syntax error

The error is: sqlite3.OperationalError: near ":id": syntax error

My search of the Official Python documentation and Google failed to find the answer:

https://docs.python.org/3/library/sqlite3.html

How should arguments be passed?

'''first create test.db:table and field
CREATE TABLE test_tab (
    id         INTEGER PRIMARY KEY ASC,
    test_num   INT,
    test_field TEXT
);
'''

import sqlite3
con = sqlite3.connect('test.db')
con.set_trace_callback(print)  # start the debug
d = [
    (111,'aaa'),
    (111,'bbb'),
    (111,'ccc'),
    (444,'ddd')
]
sql = "insert into `test_tab` (`test_num`, `test_field`) values (?,?)"
cursor = con.executemany(sql, d)
con.commit()  # Execute successfully

#####################

# wrong code begin,why sql 'in ()' is wrong?
sql = "SELECT * from `test_tab` where `test_num`=:num AND `id` in :id"
par = {'num': 111, 'id': (1,2)}  # The number of 'id' parameters is uncertain
x = con.execute(sql, par)
print(x.fetchall())

In the second query, you would need actually separate placeholders for every value in the IN clause. In addition, I would use ? here:

num = 111
ids = (1, 2)
par = (num,) + ids
sql = "select * from test_tab where test_num = ? AND id in "
in_clause = '(?' + ', ?'*(len(ids) - 1) + ')'
sql = sql + in_clause
x = con.execute(sql, par)
print(x.fetchall())

The SQL query generated by the above script is:

select * from test_tab where test_num = ? AND in (?, ?)

and we bind (111, 1, 2) to the three ? placeholders.

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