将数据插入mysql,避免使用python注入sql

o3imoua4  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(283)

这个问题在这里已经有答案了

pymysql插入不工作(3个答案)
两年前关门了。
我哪里做错了?查询成功运行,但数据库表中没有数据存储
我的代码块是

import pymysql                                                                                         
db = pymysql.connect(host='', user='', passwd='',db='')                     
cursor = db.cursor()                                                                                   
cursor.executemany("""INSERT INTO Mention ( keyword, required, optional, excluded)                     
      VALUES (%s,%s,%s,%s)""",                                                                         
     [                                                                                                 
     ("Spam","fg","gtg","uu") ,                                                                        
     ("dd","fg","gtg","uu") ,                                                                          
     ("dd","fg","gtg","uu")                                                                            
     ])                                                                                                
print(cursor.fetchmany())

sdnqo3pr

sdnqo3pr1#

建议将参数传递到execute函数中,因为它们将自动转义。
试试这个:

insert_stmt = (
  "INSERT INTO Mention ( keyword, required, optional, excluded)                     
      VALUES (%s,%s,%s,%s)"
)
data = [                                                                                                 
     ("Spam","fg","gtg","uu") ,                                                                        
     ("dd","fg","gtg","uu") ,                                                                          
     ("dd","fg","gtg","uu")                                                                            
     ]
cursor.execute(insert_stmt, data)

相关问题