Skip to content

Question about transaction #1164

Closed Answered by gordthompson
zlev-intel asked this question in Q&A
Discussion options

You must be logged in to vote

How can I prevent opening transaction by pyodbc?

Use autocommit=True. That will avoid the "outer" transaction and still allow the "inner" transaction(s) to work as expected:

import pyodbc

cnxn = pyodbc.connect(
    "DSN=mssql_199;UID=scott;PWD=tiger^5HHH", autocommit=True
)
crsr = cnxn.cursor()

table_name = "discussion_1164"
crsr.execute(f"DROP TABLE IF EXISTS {table_name}")
crsr.execute(
    f"CREATE TABLE {table_name} (id int primary key, txt varchar(50))"
)
crsr.execute(f"INSERT INTO {table_name} (id, txt) VALUES (1, 'original text')")

sql = f"""\
SET NOCOUNT ON;
BEGIN TRANSACTION;
UPDATE {table_name} SET txt = 'updated text' WHERE id = 1;
ROLLBACK;
BEGIN TRANSACTION;
INSERT INTO {t…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by zlev-intel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1161 on February 07, 2023 15:19.