首页 > 解决方案 > 多个 SQL 更新查询跨越几个函数 (pyodbc)

问题描述

我有一个 sql 数据库连接,除其他外,它具有更新(比如说 4 个)相互依赖的表的功能。我最初将查询全部放在一个updates_all_in_one运行良好的函数中。

我现在正试图将其分解为拆分函数(因为我可能需要调用部分更新并避免重写相同的代码),例如update1... update4

但是,我注意到当我使用拆分并在 function 中将它们背靠背调用时updates_call_subfunc,在调用update1之后似乎该函数的更新没有到位(因此由于未应用 update1 而遇到依赖问题)并且如果连接器类来自一个功能到第二个功能,不通信。

我在网上读了一点,似乎连接是保持打开/共享的类的一部分,但我不明白为什么顺序更新会失败。

谢谢


import pyodbc as mdb

class DbHandlerSQL(object):
    def __init__(self, dbHost, dbName, dbUser, dbPass):
        if os.name=='posix':
            connStr = ('Driver={ODBC Driver 13 for SQL Server};Server='+ dbHost +'; Database='+dbName+';UID='+dbUser+';PWD='+dbPass)
        else:
            connStr = ('Driver={SQL Server};Server='+ dbHost +'; Database='+dbName+';UID='+dbUser+';PWD='+dbPass)
        self.conn = pyodbc.connect(connStr)
        self.conn.timeout = 200 
        self.dbCursor = self.conn.cursor()

    #==================================================================
    def updates_all_in_one(self,myname, mytype, mysource, local_id):
        try:
            q1 = " update table1 \
                   set name=? , type=?, source=?  \
                   where local_id=?"
            self.dbCursor.execute(q1, (myname, mytype, mysource, local_id) )

            q2 = " update table2 \
                   set name=? , type=?, source=?  \
                   where local_id=?"
            self.dbCursor.execute(q2, (myname, mytype, mysource, local_id) )

            q3 = " update table3 \
                   set name=? , type=?, source=?  \
                   where local_id=?"
            self.dbCursor.execute(q3, (myname, mytype, mysource, local_id) )

            q4 = " update table4 \
                   set name=? , type=?, source=?  \
                   where local_id=?"
            self.dbCursor.execute(q4, (myname, mytype, mysource, local_id) )

        except:
            raise Exception('problem during uploading')    
       self.dbCursor.commit()

    #==================================================================
    def updates_call_subfunc(self,myname, mytype, mysource, local_id):
        self.update1(myname, mytype, mysource, local_id)
        ' Test 1

        self.update2(myname, mytype, mysource, local_id)
        ' Test 2

        self.update3(myname, mytype, mysource, local_id)
        ' Test 3

        self.update4(myname, mytype, mysource, local_id)
        ' Test 4

        self.dbCursor.commit()

    #==================================================================
    def update1(self,myname, mytype, mysource, local_id):
        q1 = " update table1 \
               set name=? , type=?, source=?  \
               where local_id=?"
        self.dbCursor.execute(q1, (myname, mytype, mysource, local_id) )

    #==================================================================
    def update2(self,myname, mytype, mysource, local_id):
        q2 = " update table2 \
               set name=? , type=?, source=?  \
               where local_id=?"
        self.dbCursor.execute(q2, (myname, mytype, mysource, local_id) )

    #==================================================================
    def update3(self,myname, mytype, mysource, local_id):
        q3 = " update table3 \
               set name=? , type=?, source=?  \
               where local_id=?"
        self.dbCursor.execute(q3, (myname, mytype, mysource, local_id) )

    #==================================================================
    def update4(self,myname, mytype, mysource, local_id):
        q4 = " update table4 \
               set name=? , type=?, source=?  \
               where local_id=?"
        self.dbCursor.execute(q4, (myname, mytype, mysource, local_id) )

标签: pythonpyodbc

解决方案


推荐阅读