Pravin
Pravin

Reputation: 319

Tried to update the database table values using python 'pyodbc'. But not working

I just created a new table in the database with empty columns in varchar(max) datatype. Tried to update the column values using pyodbc but the changes are not getting reflected in the database table.

Any suggestions, what am I doing wrong here?

My Code:

#Code to connect database with the notebook
conn_str = pyodbc.connect(
    r'Driver=SQL Server;'
    r'Server=ALAP;'
    r'Database=master;'
    r'Trusted_Connection=yes;'
    )

cursor = conn_str.cursor()

cursor.execute("UPDATE tbl_EMAIL_ENQUIRY SET fld_EMAIL_BODY = ? ", 'Hello')

conn_str.commit()

Upvotes: 0

Views: 284

Answers (1)

Ankiz
Ankiz

Reputation: 384

This will work, let me know, if it dosen't, give it a try!

connection_string = "r'Driver=SQL Server;'
    r'Server=ALAP;'
    r'Database=master;'
    r'Trusted_Connection=yes;'"


with pyodbc.connect(connection_string) as conn:
    with conn.cursor() as cursor:
        cursor.execute('UPDATE tbl_EMAIL_ENQUIRY SET fld_EMAIL_BODY = ?','Hello')
    cursor.close()

the only thing if it can error could be the connection string pattern, below I am sharing, how connection string looks like on my end:

connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=MyServerName;DATABASE=MyDBNAME;UID=MyID;PWD=MyPASS"

you might change it to as

connection_string = "DRIVER={SQL Server};SERVER=ALAP;DATABASE=master;Trusted_Connection=yes"

Upvotes: 1

Related Questions