pyodbc insert into sql

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] ([Message]) values('test')")

I get no error but nothing is inserted into the table.
Directly after I fetch the data the inserted row is fetched. But nothing is saved.

In MS SQL Server Management Studio the insertion does work.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to commit the data. Each SQL command is in a transaction and the transaction must be committed to write the transaction to the SQL Server so that it can be read by other SQL commands.

Under MS SQL Server Management Studio the default is to allow auto-commit which means each SQL command immediately works and you cannot rollback.

The example is from pyodbc Getting Started document

First opening the database and set up a cursor

import pyodbc

# Specifying the ODBC driver, server name, database, etc. directly
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass')

# Create a cursor from the connection
cursor = cnxn.cursor()

The insert example in the document is then

# Do the insert
cursor.execute("insert into products(id, name) values ('pyodbc', 'awesome library')")
#commit the transaction
cnxn.commit()

or better using parameters

cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')
cnxn.commit()

As the document says

Note the calls to cnxn.commit(). You must call commit or your changes will be lost! When the connection is closed, any pending changes will be rolled back. This makes error recovery very easy, but you must remember to call commit.

Method 2

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()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x