sqlite+;python使用WHERE。。。作为参数输入>;999---还是逃跑
考虑以下伪代码:sqlite+;python使用WHERE。。。作为参数输入>;999---还是逃跑,python,sqlite,Python,Sqlite,考虑以下伪代码: items = ["aaa", "bbb", "cc%'`cc"] sql = "select * from table where item IN (?)" sqlite3.execute( sql, (items, ) ) 这将失败: InterfaceError: Error binding parameter 0 - probably unsupported type. 在StackOverflow中有很多关于它的问答 您可以预先生成一个参数列表作为“?,?,?…”
items = ["aaa", "bbb", "cc%'`cc"]
sql = "select * from table where item IN (?)"
sqlite3.execute( sql, (items, ) )
这将失败:
InterfaceError: Error binding parameter 0 - probably unsupported type.
在StackOverflow中有很多关于它的问答
您可以预先生成一个参数列表作为“?,?,?…”by“,”。join(len(items)*[“?”)
除了令人尴尬和无法阅读的答案外,如果项目列表>999,它将失败
escaped_items = map(sqlite3.escape, items)
sqlite3.execute( sql, (escaped_items, ) )
构建如此大的参数列表将是低效的 只需创建一个临时表来保存值:
sqlite3.execute("CREATE TEMPORARY TABLE items(x)")
sqlite3.executemany("INSERT INTO items VALUES (?)", items);
sqlite3.execute("SELECT ... WHERE item in items")
...
sqlite3.execute("DROP TABLE items")
如果最终的项目列表包含1000个或更多元素,则不应考虑使用(…)中的列。请改用临时表。换句话说,只为少量参数预生成参数列表,最多几十个。然后,使用临时表保存所有这些值并使用联接。