跨几个函数(pyodbc)的多个SQL更新查询

时间:2019-06-06 18:40:39

标签: python pyodbc

我有一个sql db-connection,除其他外,它具有更新(相互说成4个)相互依赖的表的功能。我最初在一个函数updates_all_in_one中一起拥有所有查询,效果很好。

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

但是,我注意到当我使用拆分并在函数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) )

0 个答案:

没有答案