与变量类似的Sql语句

xra*_*alf 1 python sqlite

我在python中执行此代码

from sqlite3 import dbapi2 as sqlite

con = sqlite.connect("db.sqlite")
cur = con.cursor()
surname = "'%atton%'"
cur.execute("select id from singers where surname like :surname", locals())
cur.close()
con.close()
Run Code Online (Sandbox Code Playgroud)

这段代码之后cur.rowcount == -1但是Patton在数据库中.

我的SQL语句不好吗?

谢谢

Tho*_*ers 5

您使用的DB-API参数化(您应该使用它,不要更改它)意味着将自动引用或转义姓氏.您应该从surname字符串中删除内部引号集.

surname = "%atton%"
cur.execute("select id from singers where surname like :surname",
    dict(surname=surname))
Run Code Online (Sandbox Code Playgroud)