SQL 更新语句但使用 pyodbc [英] SQL Update statement but using pyodbc

查看:67
本文介绍了SQL 更新语句但使用 pyodbc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pyodbc 驱动程序通过 SQL 连接到 microsoft access 表.有谁知道我如何替换此表中的字段?我曾考虑删除该行然后将该行放回去,但这会由于访问中的自动编号而更改主键.

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.

我有这个用于插入进度表:

I have this for inserting 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()

那么我将如何替换这些字段.假设我想将 CockpitDrill 从2"更改为3",(它们都是字符串).

So how would I replace these fields. Let's say I wanted to change CockpitDrill from '2' to '3', (They are all strings).

任何帮助将不胜感激.

推荐答案

您可以像现在执行 INSERT 一样执行 UPDATE 语句:

You can execute an UPDATE statement just as you now execute 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()

这有帮助吗?progress_primarykey"是我为您的数据库表中的主键字段提供的假定名称.那是假设您只想更改一条记录并且您知道它的主键.

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.

这篇关于SQL 更新语句但使用 pyodbc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆