pyodbc是否会对数据库造成影响

凡人皆侑一死 2018-01-25 02:08:41
用pyodbc 执行更新语句的时候发现,如果不commit(),然后去查数据库这个表,一直查不出来,是不是数据库的表被锁了。
大神能不能帮我看看下面
def hz_channelorder():
showbill_hz.delete(1.0,"end")
#正式库
conn=pyodbc.connect(r'DRIVER={SQL Server Native Client 10.0};SERVER=localhost;DATABASE=KFT;UID=test;PWD=test##')
#测试库
#conn=pyodbc.connect(r'DRIVER={SQL Server Native Client 10.0};SERVER=localhost;DATABASE=KFT_TEST;UID=md;PWD=123')
cur=conn.cursor()
#获取输入值
cd=str(t_hz.get("1.0","end"))
cd=cd.strip()
if (cd==''):
mes.showerror("贴心小提示","未输入单号")
else:
billnos=re.findall(r'\w{2}\d{6}-\d{4}',cd)
#print(billno)
for billno in billnos:
type=billno[0:2]
if (type=='KH'):
select_sql="select billstate from kft_applynew where billno='%s'"%(str(billno))
elif (type=='WX'):
select_sql="select billstate from kft_repair where billno='%s'"%(str(billno))
else:
select_sql="select billstate from kft_exchange where billno='%s'"%(str(billno))
#print(select_sql)
cur.execute(select_sql)
test=cur.fetchone()
if (test is None):
info="%s单据在系统未找到,请检查单据是否输入错误"%(str(billno))
hz_print_info(info)
save_log(info)
else:
billstate=str(test[0])
#if (billstate=='150' or billstate=='330' or billstate=='430' or billstate=='130'):
if (billstate in ('150','130','330','430','-100','140','-300','-400')):
#可能会中断循环操作
#mes.showerror("拒绝操作","%s单据是已完成或已撤单状态,无法撤单"%(str(billno)))
info="%s单据是已完成或已撤单状态,无法撤单"%(str(billno))
hz_print_info(info)
save_log(info)
else:
update_sql="exec cd '%s'"%(str(billno))
#print(update_sql)
cur.execute(update_sql)
conn.commit()
info="%s单据已撤单"%(str(billno))
hz_print_info(info)
save_log(info)
cur.close()
conn.close()
这段代码有没有问题。最后执行的update_sql是个存储过程,代码如下:
CREATE procedure cd(@billno varchar(50))    
as
begin
declare @type varchar(20),@table varchar(50),@sql nvarchar(max),@billstate varchar(50)
set @type=left(@billno,2)
if @type='TJ' or @type='HJ' or @type='YJ' or @type='TH'
begin
set @table='kft_exchange'
set @billstate='-400'
end
else if @type='WX'
begin
set @table='kft_repair'
set @billstate='-300'
end
else if @type='KH'
begin
set @table='kft_applynew'
set @billstate='-100'
update KFT_AccountCards set IsOpen=0,OpenDate=null where CardCode in (select CardCode from kft_applynew where BillNo=@billno)
end
else
begin
print 'error:单号输入错误'
end
set @sql='update '+@table+' set billstate='+@billstate+' where billno='''+@billno+''''
exec sp_executesql @sql
end


求大神帮忙看看这个代码是不是会有问题,主要是会不会有执行到update而不commit()的可能。谢谢,谢谢。
...全文
700 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
凡人皆侑一死 2018-01-30
  • 打赏
  • 举报
回复
引用 3 楼 oyljerry 的回复:
不commit,事务不会提交吧。
是不会提交哦,但是表居然被锁了,也不释放,非得commit或者关闭应用程序才可以select 表
oyljerry 2018-01-30
  • 打赏
  • 举报
回复
引用 4 楼 verynicemen 的回复:
引用 3 楼 oyljerry 的回复:
不commit,事务不会提交吧。
是不会提交哦,但是表居然被锁了,也不释放,非得commit或者关闭应用程序才可以select 表
那要看是不是有什么互斥锁。或者存储过程有影响
oyljerry 2018-01-29
  • 打赏
  • 举报
回复
不commit,事务不会提交吧。
凡人皆侑一死 2018-01-29
  • 打赏
  • 举报
回复
随便来个人吧,我要吧帖子结掉
凡人皆侑一死 2018-01-25
  • 打赏
  • 举报
回复
顺便说下,我用的python3,数据库是sql server 2008,上面的python函数主要是先获取一个text控件的值,然后用正则刷选订单号,最后执行循环,先查单据能否在数据库找到,找不到提示未找到,找得到在执行最后的exec cd ‘单号’ 这个存储过程,提交事务,往本地的log写日志,请教下这样会不会有问题,不会对数据库造成锁表吧

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧