python 调用存储过程

发布于 2025-01-08 03:45:55 字数 2696 浏览 1 评论 0原文

我正在编写一个脚本来提取信息/更新 MsSQL 服务器,我可以让我的一个存储过程调用工作,但不能让 updateDB 函数中的第二个存储过程调用工作。这是我的代码,脚本运行良好,没有错误代码

import pyodbc 
import json
import argparse
import cgi, cgitb


#GLOBALS
    BUILDCODE = " "
    deptCODE = 0
    bldgCODE = " "
def getCodes(conn, building, department):
    #just for testing
    departmentt = 'COMPUTER SCIENCE'
    buildingt = 'PEARCE HALL'


    #geting the building code from db
    cursorBuild = conn.cursor()
    cursorBuild.execute("select * from dbo.building where name = '%s'" % buildingt)
    bldgRow = cursorBuild.fetchall() 
    cursorBuild.close()
    bldgCode = bldgRow.pop(0)

    global bldgCODE
    bldgCODE = bldgCode.code


    #getting the dept code
    cursorDept = conn.cursor()
    cursorDept.execute("execute dbo.GetCode_ByDepartment @department = '%s'" % departmentt)
    deptRow = cursorDept.fetchall()
    cursorDept.close()
    deptCode = deptRow.pop(0)

    global deptCODE
    deptCODE = deptCode.code
    print type(deptCODE)
    print deptCODE

    #returning the values
    return (bldgCode, deptCode)

    def updateDB(conn, tag, room, updater):
    #updating the database
    updateCursor = conn.cursor()

    print deptCODE
    print bldgCODE
        #this will pass params just has them hard codded for testing
    conn.execute("exec dbo.UpdateCheck_In @room = '400', @building = 'PE', @department = 307, @global_id = 'bacon', @tag = '120420'")


if __name__ == "__main__":
    #connectin to the db with SQL Authentification
    conn = pyodbc.connect(driver = '{SQL Server}', server = '(local)',
        database = 'Inventory', uid = 'sa', pwd = 'p@$$w0rd123')

#checking to see if you connected to the db or not 
    if (conn == False):
        print "Error, did not connect to the database"
    else:
        #NEEDS THIS cgitb.enable
        cgitb.enable()
        # Create instance of FieldStorage   
        form = cgi.FieldStorage()

        #get the data from the url that called it 
        tag = form.getvalue('tagnum')
        building = form.getvalue('build')
        roomNum = form.getvalue('room')
        department = form.getvalue('dept')
        updater = form.getvalue('update')

        #check to see if item is in the db 
        itemIsThere = conn.cursor()
        itemIsThere.execute("select * from dbo.check_in where tag = '120420';")
        print itemIsThere
        itemIsThere.close()
        #if the item in in the inventory 
        if (itemIsThere != None):
                #getting the codes
            getCodes(conn, building, department)
            #calling the update function
            updateDB(conn, tag, roomNum, updater)
        else :
                pass

    conn.close()

I am writing a script to pull info/update a MsSQL server and i can get my one stored procedure call to work but not my second one in the updateDB function. here is my code the script runs fine no error codes

import pyodbc 
import json
import argparse
import cgi, cgitb


#GLOBALS
    BUILDCODE = " "
    deptCODE = 0
    bldgCODE = " "
def getCodes(conn, building, department):
    #just for testing
    departmentt = 'COMPUTER SCIENCE'
    buildingt = 'PEARCE HALL'


    #geting the building code from db
    cursorBuild = conn.cursor()
    cursorBuild.execute("select * from dbo.building where name = '%s'" % buildingt)
    bldgRow = cursorBuild.fetchall() 
    cursorBuild.close()
    bldgCode = bldgRow.pop(0)

    global bldgCODE
    bldgCODE = bldgCode.code


    #getting the dept code
    cursorDept = conn.cursor()
    cursorDept.execute("execute dbo.GetCode_ByDepartment @department = '%s'" % departmentt)
    deptRow = cursorDept.fetchall()
    cursorDept.close()
    deptCode = deptRow.pop(0)

    global deptCODE
    deptCODE = deptCode.code
    print type(deptCODE)
    print deptCODE

    #returning the values
    return (bldgCode, deptCode)

    def updateDB(conn, tag, room, updater):
    #updating the database
    updateCursor = conn.cursor()

    print deptCODE
    print bldgCODE
        #this will pass params just has them hard codded for testing
    conn.execute("exec dbo.UpdateCheck_In @room = '400', @building = 'PE', @department = 307, @global_id = 'bacon', @tag = '120420'")


if __name__ == "__main__":
    #connectin to the db with SQL Authentification
    conn = pyodbc.connect(driver = '{SQL Server}', server = '(local)',
        database = 'Inventory', uid = 'sa', pwd = 'p@$w0rd123')

#checking to see if you connected to the db or not 
    if (conn == False):
        print "Error, did not connect to the database"
    else:
        #NEEDS THIS cgitb.enable
        cgitb.enable()
        # Create instance of FieldStorage   
        form = cgi.FieldStorage()

        #get the data from the url that called it 
        tag = form.getvalue('tagnum')
        building = form.getvalue('build')
        roomNum = form.getvalue('room')
        department = form.getvalue('dept')
        updater = form.getvalue('update')

        #check to see if item is in the db 
        itemIsThere = conn.cursor()
        itemIsThere.execute("select * from dbo.check_in where tag = '120420';")
        print itemIsThere
        itemIsThere.close()
        #if the item in in the inventory 
        if (itemIsThere != None):
                #getting the codes
            getCodes(conn, building, department)
            #calling the update function
            updateDB(conn, tag, roomNum, updater)
        else :
                pass

    conn.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热血少△年 2025-01-15 03:45:55

运行更新过程后提交更改:

...
#this will pass params just has them hard coded for testing
conn.execute("exec dbo.UpdateCheck_In @room = '400', @building = 'PE', @department = 307, @global_id = 'bacon', @tag = '120420'")
conn.commit()

pyodbc.connect()自动提交

Commit your changes after the update procedure is run:

...
#this will pass params just has them hard coded for testing
conn.execute("exec dbo.UpdateCheck_In @room = '400', @building = 'PE', @department = 307, @global_id = 'bacon', @tag = '120420'")
conn.commit()

In pyodbc.connect() autocommit is disabled by default

吹泡泡o 2025-01-15 03:45:55

大家都找到了答案,这是因为我为 MsSQL 2008 指定了旧版驱动程序而不是较新的驱动程序,代码如下

    conn = pyodbc.connect(driver = '{SQL Server Native Client 10.0}', server = '(local)',            database = 'Inventory', uid = 'sa', pwd = 'p@$w0rd123',autocommit=True)  

found the answer everybody, it was because I specified the legacy driver instead of the newer one for MsSQL 2008 heres the code

    conn = pyodbc.connect(driver = '{SQL Server Native Client 10.0}', server = '(local)',            database = 'Inventory', uid = 'sa', pwd = 'p@$w0rd123',autocommit=True)  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文