Durai Sankaran
Durai Sankaran

Reputation: 43

ERROR: syntax error at or near "N" in sqlalchemy.exc.ProgrammingError

I have a query as

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 = ?;
'''

By using pandas read_sql, fetching data from databaase.

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

It throws an error as sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) syntax error at or near "N" N'select ex.b_id, b.code, ..

Upvotes: 0

Views: 386

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 248135

If you read the documentation, you will see that there are no string constants of the form N'...' in PostgresSQL. All string constants have the same encoding, which is specified by the setting of client_encoding for your session.

Upvotes: 1

Related Questions