Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.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
Python PYODBC不喜欢“%”;SQL包含2个参数标记,但提供了1个参数;_Python_Sql_Pyodbc - Fatal编程技术网

Python PYODBC不喜欢“%”;SQL包含2个参数标记,但提供了1个参数;

Python PYODBC不喜欢“%”;SQL包含2个参数标记,但提供了1个参数;,python,sql,pyodbc,Python,Sql,Pyodbc,因此,我目前正在将Python与SQL链接,以提取客户信息。不幸的是,我在SQL方面遇到了一些错误。我试图使用LIKE运算符和%通配符,但我不断地遇到错误,因为Python不喜欢%。因此,它假装%s之间的变量不存在。我的意思是: SELECT custnbr, firstname, middleint, lastname FROM lqppcusmst WHERE custnbr = ? AND firstname LIKE ? 现在,我只是

因此,我目前正在将Python与SQL链接,以提取客户信息。不幸的是,我在SQL方面遇到了一些错误。我试图使用LIKE运算符和%通配符,但我不断地遇到错误,因为Python不喜欢%。因此,它假装%s之间的变量不存在。我的意思是:

SELECT custnbr,
       firstname,
       middleint,
       lastname
FROM   lqppcusmst
WHERE  custnbr = ?  AND firstname LIKE ? 
现在,我只是测试一下,所以我只是使用客户号码和名字。我给它一个值:

remote_system_account_number = request.DATA['remote_system_account_number']
remote_system_first_name = request.DATA['remote_system_first_name']
因为我写的是在数据库中搜索客户,所以可能会有空白条目,所以我有这样的内容:

if remote_system_account_number != '':
    SQL_where += ' custnbr = ? '
    parameters += "remote_system_account_number"
if remote_system_first_name != '':
    SQL_where += ' AND firstname LIKE ? '
    parameters += ", %remote_system_first_name%"
database_cursor.execute(customer_information_SQLString + SQL_where, parameters)
所以我认为这会起作用,但没有。当我这样执行它时:

if remote_system_account_number != '':
    SQL_where += ' custnbr = ? '
    parameters += "remote_system_account_number"
if remote_system_first_name != '':
    SQL_where += ' AND firstname LIKE ? '
    parameters += ", %remote_system_first_name%"
database_cursor.execute(customer_information_SQLString + SQL_where, parameters)
我明白了:

ProgrammingError: ('The SQL contains 2 parameter markers, but 1 parameters were supplied', 'HY000')

有人知道如何处理这个问题吗?

参数不应该是逗号分隔的字符串,它应该是一个可枚举的(列表或类似的)字符串,其值的数量与SQL中的占位符数量相匹配。例如:

parameters = []
if remote_system_account_number != '':
    SQL_where += ' custnbr = ? '
    parameters.append("remote_system_account_number")
if remote_system_first_name != '':
    SQL_where += ' AND firstname LIKE ? '
    parameters.append("%remote_system_first_name%")

尝试使用
%%
来转义您的
%%
符号。