Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在sql语句的python字符串中使用通配符%_Python_Sql_Python 3.x_Pymysql - Fatal编程技术网

如何在sql语句的python字符串中使用通配符%

如何在sql语句的python字符串中使用通配符%,python,sql,python-3.x,pymysql,Python,Sql,Python 3.x,Pymysql,我遇到这种情况是为了检查数据库中的一行中是否存在帐号。该列可以包含多个帐号,每个帐号用逗号分隔,例如00001000003。 例如,案例:SELECT*FROM finance\u collection.member,其中帐户ID如00001、%OR%、00001或、%00001、%OR 00001 为此,我编写了一条SQL语句,如下所示: account_exists = " SELECT * FROM finance_collection.member WHERE account_ids L

我遇到这种情况是为了检查数据库中的一行中是否存在帐号。该列可以包含多个帐号,每个帐号用逗号分隔,例如00001000003。 例如,案例:
SELECT*FROM finance\u collection.member,其中帐户ID如00001、%OR%、00001或、%00001、%OR 00001
为此,我编写了一条SQL语句,如下所示:

account_exists = " SELECT * FROM finance_collection.member WHERE account_ids LIKE %s,% OR %,%s OR %,%s,% OR %s "  
此处%s是传入帐号,是逗号,%是多字符通配符

    try:
    with connection.cursor() as cursor:

        sql = sqls.account_exists
        cursor.execute(sql, (account, account, account, account))
        return True

    except pymysql.Error as error:
    print(error)
    return False

我正在使用pymysql进行连接,但查询似乎不起作用。我尝试了各种组合,如环绕
%s、%%
'
以及其他组合,但都不起作用。你能给我看一盏灯吗?

用另一盏灯逃出%s(是的,这没什么意义,但它就是这样):


“查询不起作用。”你能更具体一点吗?错误?错误的结果?什么?删除try/except以找出整个回溯。是否尝试将“\”转义为%字符?是否尝试直接在DB中运行此查询?;)将你的
%
翻一番以逃脱它们。@AndrewPaxson这行不通。它将引发
TypeError:格式字符串的参数不足
x = "a=%sx%%2C" % ('text')
x
>>> 'a=textx%2C'