Using prepared statements with mysql in python

后端 未结 3 1831
醉酒成梦
醉酒成梦 2020-12-03 15:42

I am trying to use SQL with prepared statements in Python. Python doesn\'t have its own mechanism for this so I try to use SQL directly:

sql = \"PREPARE stm         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 16:16

    Python does support prepared statements:

    sql = "INSERT INTO {} (date, time, tag, power) VALUES (%s, %s, %s, %s);"
    sql = sql.format(self.db_scan_table)
    self.cursor.execute(sql, (d, t, tag, power))
    

    (You should ensure self.db_scan_table is not vulnerable to SQL injection)

    This assumes your paramstyle is 'format', which it should be for MySQL.

提交回复
热议问题