比如这种查询,uid 字段是 int 类型的,但是在 pymysql 的原生查询方式里,用%s 占位符,%d 占位符,int 类型,数字字符串查询:
sp.execute_cmd('select * from table where uid = %s', 12)
sp.execute_cmd('select * from table where uid = %s', '12')
sp.execute_cmd('select * from table where uid = %d', 12)
测试了一下都能查询到,问题有如下:
1
maocat 2023-01-31 00:23:07 +08:00
三条语句实际上都是 uid = 12
golang 里有 %#v 可以额外输出类型名,python 里就不知道了 |
2
centralpark 2023-01-31 00:37:50 +08:00
建议直接上 sqlalchemy ,而且用原生 sql 也不应该自己做字符串拼接,应该用 ?来做占位符。
|
3
Gakho 2023-01-31 08:38:54 +08:00
你可能需要了解一下 PEP249 的 paramstyle
|
4
lookStupiToForce 2023-01-31 11:03:10 +08:00
你如果不做类型检查和文本检查,就要小心注入攻击
|
5
duckyrain 2023-01-31 17:50:13 +08:00
def execute(self, query, args=None):
"""Execute a query :param str query: Query to execute. :param args: parameters used with query. (optional) :type args: tuple, list or dict :return: Number of affected rows :rtype: int If args is a list or tuple, %s can be used as a placeholder in the query. If args is a dict, %(name)s can be used as a placeholder in the query. """ |