我刚刚迁移到新的 venv,pymysql 版本从 v1.1.0 升级到 v1.1.1,现在当参数由
Cursor.execute
函数格式化时出现 SQL 错误
client_id = float(2)
params = (client_id)
print(f"Parameters :\n\t{params}")
try:
cursor.execute("SELECT * FROM clients WHERE id = %s", params)
except:
print("EXCEPTION")
print(f"Query :\n\t{cursor._executed}")
这是执行的查询
Parameters :
(2.0)
Query :
SELECT * FROM clients WHERE id = 2.0e0
如果我检索数据,这不是问题,但是当我插入或更新数据时,它会失败
client_id = float(2)
balance = np.float64(1000)
params = (client_id, balance)
print(f"Parameters :\n\t{params}")
try:
cursor.execute("UPDATE client_balances SET balance = %s WHERE client_id = %s", params)
except:
print("EXCEPTION")
print(f"Query :\n\t{cursor._executed}")
输出:
Parameters :
(2.0, np.float64(1000.0))
EXCEPTION
Query :
UPDATE client_balances SET balance = np.float64(1000)e0 WHERE client_id = 2.0e0
如果我检查异常,我会收到以下错误
ProgrammingError - (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'e0, balance = np.float64(1000)e0, WHERE client_id = 2.0e0' at line 1")
client_id = float(2)
balance = np.float64(1000)
params = (client_id, balance)
print(f"Parameters :\n\t{params}")
refactored_params = []
for param in params:
refactored_params.append(str(param))
try:
cursor.execute("UPDATE client_balances SET balance = %s WHERE client_id = %s", refactored_params)
except:
print("EXCEPTION")
print(f"Query :\n\t{cursor._executed}")
输出:
Parameters :
(2.0, np.float64(1000.0))
Query :
UPDATE client_balances SET balance = '1000' WHERE client_id = '2.0'
execute
之前手动转换参数,但由于在更改 venv 之前我没有任何问题,所以我想了解我是否做错了什么,而不是制定一个不正确的解决方法真的很有效率。而且我不敢相信pymysql无法正确地将参数转换为查询,所以我想我肯定做错了什么。
对于这个问题有什么想法吗?我应该手动强制字符串转换还是更改实现?
Parameters :
(2.0, np.float64(1000.0))
Maybe, it should be:
(2.0, 1000.0)
我不确定。这可能是“numpy”的问题。