发布网友 发布时间:2022-04-27 09:49
共1个回答
热心网友 时间:2022-04-08 04:03
可以使用如下方法:存储过程定义基本如下:ALTER procere [dbo]. [mysp] @Station varchar( 50), @SN varchar( 50), @Info varchar( 500)output, @Msg varchar( 500)output 1. 使用adodbapiPython代码 from adodbapi import connect server = 'dbserver' user = 'username' password = 'password' database = 'database' sp = 'sp' station = 'station' sn = 'sn' try : db = connect('Provider=SQLOLEDB.1 ;Data Source=%s;Initial Catalog=%s;/ User ID=%s;Password=%s;'%(server, database, user, password)) except Exception, e: print e else : cur = db.cursor() msg = cur.callproc(sp, (station, sn)) #参数与存储过程有关 if len(msg) > 1 : if msg[-1 ] is None : print 'sn is ok. Can be test at this station' else : print msg[-1 ] finally : try : db.close() except : pass 2. 使用pymssqlPython代码 from pymssql import connect server = 'dbserver' user = 'user' password = 'password' database = 'database' sp = 'sp' station = 'station' sn = 'sn' sql = ['set nocount on' ] sql.append('declare @Msg varchar(500)' ) sql.append('declare @return_value varchar' ) sql.append("exec @return_value = %s @Station = '%s', @SN = '%s', @Info = '@Info', @Msg = @Msg output" ) sql.append('select @Msg, @return_value' ) sql = '/n' .join(sql) % (sp, station, sn) def ffchk(server, user, password, database, sql): try : db = connect(host = server, database = database, user = user, password = password, login_timeout = 10 ) cur = db.cursor() cur.execute(sql) except Exception, e: print e else : cur.nextset() # 要加上这句才能通过fetch函数取到值 注意:这语句在Python2.7对应的pymssql版本中是错误!!!