python - 在Python中运行sql server查询时出现错误

标签 python sql-server django azure pyodbc

我正在使用Python的Django框架构建一个Web应用程序,数据库是Sql服务器Azure,使用Pyodbc。

我有一个无法解决的问题,我得到一个 parameter(file_name) 这是一个字符串,当使用该参数运行查询时,我在第一种情况下得到空结果,

  "sql = sql % tuple('?' * len(params))
            TypeError: not all arguments converted during string formatting"

在第二种情况下,我做错了什么?

file_name=request.POST.get('filetodelete')
    with connections['default'].cursor() as c:
       1st case
          parms=[file_name]
          sql='select *  from banks_row_data where file_name=%s'
          c.execute (sql,parms)
          test=c.fetchall()
      2nd case
          c.execute (sql,file_name)
          test=c.fetchall()

最佳答案

pyodbc 使用 ? 作为参数占位符,而不是 %s

sql = 'select *  from banks_row_data where file_name = ?'

请注意,由于您使用的是 Django,因此您确实应该使用 Django 模型层进行此类查询。

关于python - 在Python中运行sql server查询时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46212592/

相关文章:

python - 在字典键中搜索元素

python - 将一个元素列表交换为 int

python - 修改 Pandas 数据框以列出年月日

sql-server - SQL Server 中表的 sp_tableoption 行内行为

sql-server - 在 PostgreSQL 中使用 plperl 函数返回 MSSQL 数据

python - 在 Django 中放置额外启动代码的正确位置?

python - 包含 Django 应用程序的 url.py 会导致 404

python - 函数参数 Python 的多个默认值

sql-server - 临时存储上的 Azure VM SQL Server Tempdb

python - Collectstatic 在使用 s3boto3 和 manifestfilemixin 时给出 NoSuchKey