python - 多个 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) )
解决方案
推荐阅读
- css - 弹性项目的宽度正在缩小
- c++ - 指向派生类的抽象bass类的shared_ptr向量
- javascript - 阅读更多切换按钮
- typo3 - 更新 Typo3 8.7.31 后 - 后端没有页面树
- r - 根据数据分布寻找合适的聚类方法
- kibana - 将 OpenID 用户添加到 Open Distro Kibana
- android - 如何在颤动的降价中添加超链接到文本
- r - ggplot2 中 annotate() 的默认单位是什么?
- node.js - 命名空间中的所有套接字是否都连接到 socket.io 中服务器上的同一端口?
- node.js - 为什么在 docker run as root 中创建具有不同所有权的文件/文件夹?