首页 > 解决方案 > 无法在 python 中执行长 sql 语句?

问题描述

我已将我的 sql 语句定义如下(注意换行符)

  def getTankSystemIds():
        sql='select tt.TankSystemId,ts.sitecode,tt.productid ' \
            'from  [dbo].[TankSystems] tt' \
            'left join [dbo].sites ts on tt.siteid=ts.siteid where ts.companyid=8' \
            'and ProductId in (10,4,2,3,11,4)'
        cursor = connectDB()
        cursor.execute(sql)
        records = cursor.fetchall()

调用此方法时,出现以下错误。我将单引号更改为 " 和 ''',""" 但都给出了相同的错误我在这里做错了什么?我也尝试在一条长行中提供 sql 语句。但同样的错误

SyntaxError: Non-ASCII character '\xef' in file /Users/ratha/PycharmProjects/Processor/Utilities/DbConnector.py on line 86, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

标签: pythonpython-2.7

解决方案


SQL 查询是从某处复制/粘贴还是被另一个程序保存为 UTF-8 编码?它有一个字节顺序标记(BOM),这就是\xef字符。ASCII 不喜欢这样,因为它是 Unicode 的东西。


推荐阅读