0

I'm trying to commit changes to a database in my python scripts using a sqlalchemy engine, this works fine on my normal PC (and my laptop) but i'm now trying to get the script to work on a vm, so i can automate it without it getting in the way of my normal work. I think my venv is set up properly, and i've downloaded ODBC Driver 17 for SQL Server but i keep getting this error when i try to write to the database:

sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM012', '[IM012] [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error (0) (SQLDriverConnect)')

My connection string is in the format below, and as i mentioned, works perfectly on 2 other PCs, and i've tried so many different variations for writing the driver name (with '+' between each word, and with {} surrounding it) and i still can't get it to work.

engine_string = r"mssql+pyodbc://Username:Password" \
                        r"@azure.database.windows.net/database_name?" \
                        r"driver=ODBC Driver 17 for SQL Server;" \
                        r"MARS_Connection=yes;" \
                r"pool_pre_ping=True

I've tried all the solutions i could find in a few hours of googling, but nothing seems to work. I've also tried removing the mars connection and pool pre ping bits although, as mentioned, they do normally work.

Thanks

Liam
  • 15
  • 5
  • 1
    Try using a [Pass through exact Pyodbc string](https://docs.sqlalchemy.org/en/14/dialects/mssql.html#pass-through-exact-pyodbc-string) – Gord Thompson Jun 25 '21 at 15:27
  • @GordThompson thanks so much, this works! If you want to write it up as answer, I will accept it :) – Liam Jun 25 '21 at 15:41

0 Answers0