I want to read a table from a query script in my computer.
I found the command to be simple:
with open(sql_file, 'r') as opened:
query = alq.text(opened.read())
The first issue, has to do with encoding:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81
in position 7963: character maps to <undefined>
So we do what we do, search for an answer to add argument into command open(sql_file, encoding='utf8')
and execute the query.
engine.execute(query)
And now the error is more tricky:
ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000]
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]
Incorrect syntax near '\ufeff'. (102) (SQLExecDirectW)")
[SQL: "\ufeffWITH\n-- Some comment about the query\nQC_SCORE AS
(\n SELECT some_table.id\n , other_table.column\n
That is to not be reading the file correctly. Upon inspection I found that there is \ufeff
at the beginning of the file and that the line breaks \n
are not being translated.
Does anyone know how to go about it?