【问题标题】:Inserting data to mysql, avoiding sql injection using python [duplicate]将数据插入mysql,避免使用python进行sql注入[重复]
【发布时间】:2018-08-10 04:35:05
【问题描述】:

我哪里做错了?查询运行成功,但数据库表中没有数据存储

我的代码块是

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())   

【问题讨论】:

  • 您有没有遇到任何错误?你说“没有存储数据”是因为打印不返回任何内容或为什么?我运行了您的代码并存储了数据,但打印没有返回任何内容
  • 我认为这与 SQL 注入无关。您似乎正确使用了参数。但是你没有提交事务,pymysql也没有隐式提交。

标签: python mysql sql-injection


【解决方案1】:

建议将参数传递给执行函数,因为它们会自动转义。

试试这个:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多