因此,我目前正在将 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)
我明白了:
ProgrammingError: ('The SQL contains 2 parameter markers, but 1 parameters were supplied', 'HY000')
有人知道如何处理这个问题吗?