How to properly escape strings when manually building SQL queries in SQLAlchemy?

前端 未结 4 1052
我在风中等你
我在风中等你 2021-02-14 15:29

I am using SQLAlchemy to connect to different databases in Python, but not with the ORM support as this cannot be implemented due to several reasons.

Mainly I do build a

4条回答
  •  Happy的楠姐
    2021-02-14 16:16

    You can use escape_string method from pymysql and then escape : so SQLAlchemy won't try to bind parameter for that variable, here is the sample

    import MySQLdb
    query = """ insert into.... values("{}"...) """.format(MySQLdb.escape_string(item).replace(':','\:'))
    

    Note that if you use this way your code vulnerable to SQL Injection to install pymysql

    pip3 install pymysql
    

提交回复
热议问题