在LIKE中使用通配符替换Python SQLite参数

atc*_*uno 35 python sqlite sql-like

我试图使用Python的Sqlite库进行参数化LIKE查询,如下所示:

self.cursor.execute("select string from stringtable where string like '%?%' and type = ?", (searchstr,type))
Run Code Online (Sandbox Code Playgroud)

但是?没有评估通配符内部,但是我发现此错误:

"sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied."
Run Code Online (Sandbox Code Playgroud)

我还尝试使用标记版本的查询:

like '%:searchstr%' 并在列表中 {"searchstr":searchstr...

但是,当我这样做时,查询运行但从不返回任何结果,即使手动"like '%a%'"...返回数百个结果应该

有什么建议吗?

Ale*_*lli 69

引号保护或者?还是:name被视为一个占位符-他们从字面上.您需要在所传递的字符串周围放置百分号,并使用没有引号的普通占位符.即:

self.cursor.execute(
  "select string from stringtable where string like ? and type = ?",
  ('%'+searchstr+'%', type))
Run Code Online (Sandbox Code Playgroud)

请注意,两者都不?在引号中 - 这与它们作为占位符应该是完全一样的.