【问题标题】:How to put variable in `pyodbc` query?如何在`pyodbc`查询中放入变量?
【发布时间】:2019-01-13 05:18:54
【问题描述】:

我正在使用pyodbc 在 Microsoft SQL Server 中保存数据。

I 3 个字符串变量,user_first_nameuser_last_nameprofile_photo_path

user_first_name = 'Shams'
user_last_name = 'Nahid'
file_name = 'my_profile_photo_path'

现在当我尝试使用保存数据时

cursor.execute(f'''
                    INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
                    VALUES
                    ({user_first_name}, {user_last_name}, {file_name})

                    ''')

我收到以下错误

无效的列名\'Shams\'。

无效的列名\'Nahid\'。

无法绑定多部分标识符“directory.PNG”。

但不是变量,如果我把硬编码的字符串放进去

cursor.execute('''
                    INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
                    VALUES
                    ('my_user_first_name', 'my_user_last_name', 'my_file_name')

                    ''')

然后查询显示没有错误。

我检查了变量类型,user_first_nameuser_last_namefile_name,都是<class 'str'>

如何将变量放入查询中?

【问题讨论】:

    标签: python python-3.x pyodbc


    【解决方案1】:

    使用字符串格式将列值插入 SQL 语句是一种危险的做法,因为它会使您的代码暴露于 SQL 注入 漏洞。例如,your answer 中的代码适用于

    user_last_name = "Nahid"
    

    但它会失败

    user_last_name = "O'Connor"
    

    SQL 注入也会产生严重的安全隐患。对“Little Bobby Tables”执行网络搜索以查看相关示例。

    相反,您应该像这样使用参数化查询

    user_first_name = 'Shams'
    user_last_name = 'Nahid'
    file_name = 'my_profile_photo_path'
    
    sql = '''\
    INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
    VALUES (?, ?, ?)
    '''
    params = (user_first_name, user_last_name, file_name, )
    cursor.execute(sql, params)
    

    【讨论】:

      【解决方案2】:

      我再试一次,它是工作,例如

      import pymysql
      
      def test():
          db = pymysql.connect('localhost', 'root', '****', 'python')
          cur = db.cursor()
          id_ = "123456"
          query = ''.join(["select *", " from customer where id = ", id_])
          cur.execute(query)
          result = cur.fetchone()
          print("result: ", result)
      if __name__ == '__main__':
          test()
      

      【讨论】:

        【解决方案3】:

        通过在变量两边加上引号来解决。

        cursor.execute(f'''
                            INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
                            VALUES
                            ('{user_first_name}', '{user_last_name}', '{file_name}')
        
                            ''')
        

        【讨论】:

          【解决方案4】:

          或者你可以试试这个

          cursor.execute('''
                              INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
                              VALUES
                              ("Shams", "Nahid", "my_profile_photo_path")
          
                              ''')
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-11-14
            • 2015-12-04
            • 1970-01-01
            • 1970-01-01
            • 2019-04-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多