2012-02-23 3 views
8

정보를 가져 오거나 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 = '[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'의 차이점은 무엇입니까? –

+0

그들은 둘 다 똑같은 일을합니다. – user1229126

+0

결과를 인쇄한다면 bldgRow와 deptRow는 무엇을 얻나요? –

답변

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

@beargle에서 언급 한 것처럼 "autocommit = True"를 추가했습니다. – Manfre

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에서 기본적으로 사용되지 않습니다