2010-11-08 189 views
1

这条SQL语句有什么问题?我得到一个SQLError: near "?": syntax errorSqlite语句语法错误

'select all counts from table as table where offset in ?' 

The?有一个数字与其中的列表绑定:(1,2,4)

+0

看看格式化的方式,我猜这是用python编写的。 – Varriount 2010-11-08 20:59:24

+0

你确定你的绑定正在绑定吗? – 2010-11-08 21:07:21

回答

1

只是猜测,你正在使用的语言是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,)] 
1

我想你想'select count(*) from table where offset in ?'

0

你可以绑定一个在列表到一个参数占位这样呢?您可能会考虑这种方法:创建一个临时表,将列表中的值插入临时表中,然后在表和相关列上的临时表之间进行内部联接。总的来说,比使用(?,?,?,?)子字符串构建查询语句字符串具有所需数量的问号更清晰且更易维护。