python - SQLite语句语法错误

标签 python sqlite

这个 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/

相关文章:

iphone - 无法刷新i​​Phone sqlite3数据库

python - xlwings可以在后台运行excel吗?

python - python : BS, selenium 中的网页抓取,无错误

python - 有没有办法在 python selenium 中使用 send_keys 更改文本的颜色?

Android SQLite INSERT 导致应用程序崩溃

java - 将 Sugar ORM 与 UUID 或 Joda-Time 结合使用

python - 可以自动将数据从 memcached 传输到 mysql DB 吗?

Python:在异常时继续迭代for循环

c++ - cmake:库依赖于另一个库(sqlite3pp)

ios - 在 iOS 上使用 fmbd 删除 SQLite 索引失败并出现 SQLITE_LOCKED