这个 SQL 语句有什么问题?我收到一个SQLError:near "?":语法错误
。
'select all counts from table as table where offset in ?'
那个?有一个数字绑定(bind),其中有一个列表:(1,2,4)
。
最佳答案
只是猜测您使用的语言是 Python...
无论哪种语言,原理都是相同的:
您需要动态创建适当数量的占位符。
>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> c = conn.cursor()
>>> c.execute('create table test (id int)')
<sqlite3.Cursor object at 0x011A96A0>
>>> c.executemany('insert into test values (?)', [(1,),(2,),(4,)])
<sqlite3.Cursor object at 0x011A96A0>
>>> ids = (1,2,4)
>>> query = 'select * from test where id in (%s)' % ','.join('?'*len(ids))
>>> query
'select * from test where id in (?,?,?)'
>>> c.execute(query, ids).fetchall()
[(1,), (2,), (4,)]
关于python - SQLite语句语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4127993/