如何在不确定数量的参数上使用 LIKE 和 % with pymssql?

how to use LIKE and % with pymssql on an undetermined number of parameters?

我正在使用 pymssql 连接到数据库。 该数据库有一个 table,在 上有一个列(我们称之为 col),所有字符串都是 64 个字符长。 (例如:"John ")。 现在我想在数据库中找到 John。我尝试使用 LIKE。 当我对名称进行硬编码时,它可以完美地工作:

cursor.execute("SELECT * FROM table WHERE col LIKE 'John%' ") // gives perfect results

但是当我尝试使用 %s 时,这似乎什么都不匹配。

cursor.execute("SELECT * FROM table WHERE col LIKE '%s%' ",(0,"John")) // gives Nothing
cursor.execute("SELECT * FROM table WHERE col LIKE '%s%' ",{0:"John"}) // gives SQL "Incorrect syntax" error
cursor.execute("SELECT * FROM table WHERE col LIKE '%s%' " % {0:"John"}) // gives unsupported format error

如果我只想匹配一列,这会起作用:

cursor.execute("SELECT * FROM table WHERE col LIKE '%s%' ", "John") // gives all Johns

但我想匹配不确定数量的列。 有没有办法查看最终查询的外观,以便我可以尝试自己调试它。或者你知道如何使用许多参数。 在查看 here 之后,我似乎应该使用一个元组数组,但我找不到让它工作的方法。

提前致谢

如果我懒惰的话,我会使用 format() 来写这篇文章

cursor.execute("SELECT * FROM table WHERE col LIKE '{0}%' ".format("John") )

或者更正确地使用参数:

cursor.execute("SELECT * FROM table WHERE col LIKE '%s%' ", ("John"))

我认为问题在于 (0, "John") 被解释为两个参数,而不是一个。

试试这个:

cursor.execute("SELECT * FROM table WHERE col LIKE %s ", "John%")