2012-02-23 434 views
8

我正在編寫一個腳本來拉取信息/更新MsSQL服務器,我可以讓我的一個存儲過程調用工作,但不是我的第二個updateDB函數。這裏是我的代碼腳本運行正常無故障代碼調用存儲過程python

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 = '[email protected]$$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() 
+0

'exec'和'execute'有什麼區別? – 2012-02-23 19:06:09

+0

他們都做同樣的事情 – user1229126 2012-02-23 19:33:14

+0

如果你打印結果bldgRow和deptRow你會得到什麼? – 2012-02-23 20:53:33

回答

1

找到了答案大家,這是因爲我指定了舊版驅動程序,而不是較新版本的MsSQL 2008,因此代碼爲

conn = pyodbc.connect(driver = '{SQL Server Native Client 10.0}', server = '(local)',   database = 'Inventory', uid = 'sa', pwd = '[email protected]$$w0rd123',autocommit=True) 
+1

您還添加了「autocommit = True」,這是更可能的修復方法,正如@beargle所述。 – Manfre 2014-07-22 15:37:41

5

更新程序運行後,提交更改:

... 
#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()autocommit默認情況下禁用