I am using a pyodbc driver to connect to a microsoft access table using SQL. Does anyone know how I go about replacing fields within this table?? I have though about deleting the row and then putting the row back but that would change the primary key due to the autonumber in access.
我使用pyodbc驱动程序使用SQL连接到微软访问表。有谁知道如何更换此表中的字段?我有关于删除行然后将行放回但但由于访问中的自动编号而改变了主键。
I have this for inserting into the Progress table:
我有这个插入Progress表:
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 would I replace these fields. Let's say I wanted to change CockpitDrill from '2' to '3', (They are all strings).
那么我该如何替换这些字段呢?假设我想将CockpitDrill从'2'改为'3',(它们都是字符串)。
Any help would be greatly appreciated.
任何帮助将不胜感激。
1 个解决方案
#1
12
You can execute an UPDATE statement just as you now execute your INSERT:
您可以像现在执行INSERT一样执行UPDATE语句:
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 that help? "progress_primarykey" is the assumed name I've given to the primary key field in your database table. That's supposing you just want to change one record and you know its primary key.
这有帮助吗? “progress_primarykey”是我给数据库表中主键字段的假定名称。这假设您只想更改一条记录,并且您知道它的主键。
#1
12
You can execute an UPDATE statement just as you now execute your INSERT:
您可以像现在执行INSERT一样执行UPDATE语句:
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 that help? "progress_primarykey" is the assumed name I've given to the primary key field in your database table. That's supposing you just want to change one record and you know its primary key.
这有帮助吗? “progress_primarykey”是我给数据库表中主键字段的假定名称。这假设您只想更改一条记录,并且您知道它的主键。