Skip to content
Advertisement

Python SQL: Error reading and executing SQL file

I’m trying to red and execute a SQL file in Python using sqlalchemy. Should be simple, right?

I get this error

Why am I getting this error? I’m not sure if this is a file encoding error or a SQLAlchemy error. Ideally, this should be simple.

EDIT:

This code works fine, assuming the table temp exists:

EDIT 2:

For reference, here is a link to the file toy_example.sql.
http://s000.tinyupload.com/index.php?file_id=62746453331292257227

Advertisement

Answer

(I saved it as encoded UTF-8 codepage 65001 in SQL Server Management Studio)

The UTF-8 option near the top of the “Encoding” list in the SSMS “Advanced Save Options” dialog is “UTF-8 with signature”

enter image description here

That option will write the Unicode BOM (Byte Order Mark), encoded as xEFxBBxBF, at the beginning of the file

enter image description here

If we read the file in Python using the standard “utf-8” encoding we get the Unicode BOM character ufeff included in the string

However, if we read the file using Python’s “utf-8-sig” encoding then we get the string with the BOM character removed

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement