兄弟们, pymsql 的查询里,占位符需不需要根据查询字段的类型去更换 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dafuyang
V2EX    Python

兄弟们, pymsql 的查询里,占位符需不需要根据查询字段的类型去更换

  •  
  •   dafuyang 2023-01-30 23:16:56 +08:00 2132 次点击
    这是一个创建于 986 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如这种查询,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. 第一二种这种查询是不是就会触发数据库的类型隐式转换
    2. 想了解一下占位符需不需要根据查询字段的类型去更换呢
    5 条回复    2023-01-31 17:50:13 +08:00
    maocat
        1
    maocat  
       2023-01-31 00:23:07 +08:00
    三条语句实际上都是 uid = 12

    golang 里有 %#v 可以额外输出类型名,python 里就不知道了
    centralpark
        2
    centralpark  
       2023-01-31 00:37:50 +08:00
    建议直接上 sqlalchemy ,而且用原生 sql 也不应该自己做字符串拼接,应该用 ?来做占位符。
    Gakho
        3
    Gakho  
       2023-01-31 08:38:54 +08:00
    你可能需要了解一下 PEP249 的 paramstyle
    lookSupiToForce
        4
    lookStupiToForce  
       2023-01-31 11:03:10 +08:00
    你如果不做类型检查和文本检查,就要小心注入攻击
    duckyrain
        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.
    """
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5458 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 01:24 PVG 09:24 LAX 18:24 JFK 21:24
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86