I'm trying to run a query saved in an SQL file that's encoded in UTF-8. My code is as follows:
import pyodbc
import pandas as pd
query = open('test.sql', 'r')
print(query.read())
server = 'server'
database = 'database'
username = 'username'
password = 'password'
cnxn = pyodbc.connect(
'DRIVER={ODBC Driver 17 for SQL Server};' +
'SERVER=' + server + ';DATABASE=' + database +
';UID=' + username + ';PWD='+ password
)
df = pd.read_sql_query(query.read(), cnxn)
query.close()
print(df.head())
print(query.read())
works fine and correctly shows the contents of test.sql
, but read_sql_query
gives the following error:
pyodbc.Error: ('HY090', '[HY090] [Microsoft][ODBC Driver Manager] Invalid string or buffer length (0) (SQLExecDirectW)')
If I change the encoding of the SQL file from UTF-8 to ANSI in Notepad++ before running, the code works fine with no error. How can I make this work without changing the encoding of the SQL file beforehand? I think maybe I should use .encode()
or .decode()
but I can't figure out how.