Search code examples
python-3.xsql-serverdataframeazurepyodbc

Python pyodbc write to Microsoft Azure SQL Database Error


I used to use Python pyodbc to write data to sql server Express, but Express reached 10B max disk, so I have to switch to Microsoft Azure SQL Database, I encountered below error

ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Reference to database and/or server name in 'master.dbo.syscharsets' is not supported in this version of SQL Server.

the code I using

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=192.111.111;DATABASE=MYDB;UID=' + keyring.get_password("local", "UID") + ';PWD=' + keyring.get_password("local", "PWD") + ';')

cursor = conn.cursor()

for index, row in df_add.iterrows():
    
    cursor.execute("INSERT INTO mytable([ID],[CreateDate]) values(?,?)",row['ID'], row['CreateDate']) 

conn.commit()
cursor.close()
conn.close()

Solution

  • thanks for the Microsoft team's answer, here's the step.

    1.download the new driver and install in your computer https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver16

    1. modify this part in my code , reference python code to connect from Microsoft

      DRIVER={ODBC Driver 18 for SQL Server}

    enter image description here