【问题标题】:ERROR: syntax error at or near "N" in sqlalchemy.exc.ProgrammingError错误:sqlalchemy.exc.ProgrammingError 中“N”处或附近的语法错误
【发布时间】:2019-03-03 12:32:37
【问题描述】:

我有一个查询

query = '''
EXECUTE sp_executesql
N'select ex.b_id, b.code, 
    b.status as status, 
    ex.c_id, c.c_no, c.title as title,
    ex.s_id, s.s_no, s.title as s_title,
    ex.o_id, isnull(o.o_no, o._id) as o_no,
    ex.e_id, 
    ex.types
  from dbo.exercises ex
    left join XXXX o on
      (ex.b_id = o.b_id and ex.c_id = o.c_id
       and ex.s_id = o.s_id and ex.o_id = o.o_id)
    inner join YYYY b on (ex.b_id = b.b_id)
    inner join ZZZZ c on (ex.b_id = c.b_id and
      ex.c_id = c.c_id)
    inner join SSSS s on (ex.b_id = s.b_id and
      ex.c_id = s.c_id and ex.s_id = s.s_id)
  where
    -- cleaning criteria
    -- interesting data selection
    ex.b_id = @bid
  order by ex.b_id, ex.c_id, ex.s_id, ex.o_id,
    ex.o_no, ex.e_id',
N'@bid int',
@bid = ?;
'''

通过使用pandas read_sql,从数据库中获取数据。

from sqlalchemy import create_engine 
from sqlalchemy import event 
import pandas as pd 
pd.read_sql(query, conn, params=params, chunksize=None)

它会抛出一个错误,因为 sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) 在“N”处或附近出现语法错误 N'select ex.b_id, b.code, ..

【问题讨论】:

  • 这是一个 SQL 错误。为什么要以EXECUTE sp_executesql ... 开头查询?
  • 这看起来像是 Postgresql 和 MSSQL 的混搭。据我所知,Postgresql 没有 N'' 字符串常量。 dbo.exercises 似乎也是 MSSQL 式的。

标签: python postgresql sqlalchemy


【解决方案1】:

如果您阅读the documentation,您会发现 PostgresSQL 中没有N'...' 形式的字符串常量。所有字符串常量都具有相同的编码,由会话的client_encoding 设置指定。

【讨论】:

    猜你喜欢
    • 2018-04-21
    • 2020-08-20
    • 2017-02-20
    • 2017-07-15
    • 2010-12-24
    • 2016-07-10
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多