SQLite3查询报错ValueError: parameters are of unsupported type原因排查
问题分析:SQLite3参数报错
ValueError: parameters are of unsupported type 场景复现
用户创建的SQLite3表结构:
CREATE TABLE packages ( id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL, description TEXT, price DECIMAL(5,2), enabled INTEGER DEFAULT 1 );
用于查询的Python代码(报错版本):
import sqlite3 from flask import current_app, g def connection(): if 'db' not in g or not isinstance(g.db, sqlite3.Connection): g.db = sqlite3.connect(current_app.config.get('DBPATH'), detect_types=sqlite3.PARSE_DECLTYPES) g.db.row_factory = sqlite3.Row return g.db packageid = 6 cur = connection().cursor() cur.execute(""" SELECT id FROM packages WHERE id = :packageid """, {'packageid', packageid}) # 此行报错 rec = cur.fetchone() # Omitted logic to handle rec being None or not None
报错原因
报错和id的类型完全无关,问题出在参数传递的格式错误:
- 你使用了集合
{'packageid', packageid}作为参数,但SQLite的命名参数(:packageid形式)要求传入的是键值对结构的字典。 - 集合是无序且无键值映射的结构,sqlite3模块无法解析这种类型的参数,因此抛出
ValueError: parameters are of unsupported type。
修正方案
将参数从集合改为字典(把逗号替换为冒号):
cur.execute(""" SELECT id FROM packages WHERE id = :packageid """, {'packageid': packageid})
内容的提问来源于stack exchange,提问作者Shane Bishop
相关产品推荐
相关产品推荐

