来自变量

问题描述

sql语句 我正在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()
代码“ 1”之后,但是Patton在数据库中。 我的sql语句不好吗? 谢谢     

解决方法

您使用的DB-API参数化(应使用,请勿更改)意味着该姓氏将被自动引用或转义。您应该从
surname
字符串中删除内部引号。
surname = \"%atton%\"
cur.execute(\"select id from singers where surname like :surname\",dict(surname=surname))