pyodbc execute SQL code

别等时光非礼了梦想. 提交于 2019-12-06 15:37:20

You have 2 issues:

  1. query is a tuple. The way to execute a parameterized query is as follows:

    query = """SELECT ?,count(*)
               FROM ?
               WHERE ?=? """
    args = ('date', 'myTable', 'date', '2017-05-08')
    cursor.execute(query, args)
    

    You could pass query with *. This would expand query to a string and a tuple which is what execute expects:

    cursor.execute(*query)  # 'query' here is defined as it is in your example
    
  2. But, that won't work. You can not use parameterized query to use parameters in the select and from clauses. You can also not use parameters for the column name in the where clause.

You (usually) don't have to worry about SQL injection if the value isn't inputted by the user (or if the user can't change it in anyway).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!