I use a MS SQL express db. I can connect and fetch data. But inserting data does not work:
cursor.execute(\"insert into [mydb].[dbo].[ConvertToolLog] ([Messa
Same issue here, slight modification to answers above. I struggled with this for a while, realized the SQL was running but not committing. My environment is Python 3.8.2, MSSQL Server Express 15.0.2070.41 (2019). I had to use cursor.commit(), and it worked as expected.
import pyodbc
# cnxn info is in a text file so I can change it in one place
def cnxn():
f=open("sql_conn.csv")
sql_split=f.read().split(",")
server=sql_split[0]
database=sql_split[1]
uid=sql_split[2]
pwd=sql_split[3]
return pyodbc.connect('DRIVER={ODBC Driver 17 for SQL
Server};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % (server,database,uid,pwd))
# sql statement is passed by .py script
def f_sql_insert(sql):
cursor = cnxn().cursor()
cursor.execute(sql)
cursor.commit()
cnxn().close()