SQL Update but using pyodbc

I am using the pyodbc driver to connect to a Microsoft access table using SQL. Does anyone know how I'm going to replace the fields in this table? I have at least about deleting a row and then returning the row, but that will change the primary key due to access to the auto number.

I have this to insert into the Progress table:

cnxn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=C:\\Users\\...............(file location)') cursor = cnxn.cursor() cursor.execute("insert into Progress(CockpitDrill,Mirrors,MoveOff,TurnLeft) values (?,?,?,?)",cockpit,mirrors,moveOff,turnLeft,) cnxn.commit() 

So, how to replace these fields. Say I wanted to change CockpitDrill from '2' to '3', (all of them are strings).

Any help would be greatly appreciated.

+6
source share
1 answer

You can execute the UPDATE statement just as you are now executing your INSERT:

  cnxn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=C:\\Users\\...............(file location)') cursor = cnxn.cursor() cursor.execute("UPDATE progress SET CockpitDrill = ? WHERE progress_primarykey = ?", newcockpitdrillvalue, oldprimarykeyvalue) cnxn.commit() 

Does it help? "progress_primarykey" is the intended name that I assigned to the first key in your database table. Suppose you just want to change one entry and you know its primary key.

+10
source

Source: https://habr.com/ru/post/981021/


All Articles