2017-08-04 75 views
1

我在使用函數啓動程序時無法連接到數據庫。如果我不用一個函數啓動它,它可以正常工作。 我的程序從serverlist.txt獲取一個計算機名,並在數據庫中查找它。然後它爲我提供該計算機的「位置ID」。如何與Pyodbc功能連接?

這個版本的作品:

import os 
import shutil 
import fileinput 
import pypyodbc 

def replaceid(servername): 
    try: 
     cursor = connection.cursor() 

     SQLCommand = ("SELECT Name, Location_ID " 
      "FROM dbo.I_Location " # table name 
      "with (nolock)" 
      "WHERE Name = ?") 
     Values = [servername] 
     cursor.execute(SQLCommand,Values) 
     results = cursor.fetchone() 
     if results: 

      print (" Name: " + results[0] + " Location ID: " + str(results[1])) 
      print (" ") 
     else: 
      print (" Location ID for " + servername + " does not exist.") 
      print (" ") 
      connection.close() 
    except: 
     print("Database is down or you are not connected to network.") 
     exit() 

def grab(servername): 
# copy config from remote computer 

    source = r'//' + servername + '/c$/Administrator/' 
    dest = "." 
    file = "Admin.config" 
    if os.path.isfile(os.path.join(source, file)) 
     try: 
      shutil.copyfile(os.path.join(source, file), os.path.join(dest, file)) 

     except: 
      print (" Local directory you are copying to does not exist.") 
    else: 
     pass 

    replaceid(servername) 



os.system('cls' if os.name == 'nt' else 'clear') 
array = [] 
with open("serverlist.txt", "r") as f: 
    for servername in f: 

     try: 
      connection = pypyodbc.connect('Driver={SQL Server};Server=mydbx;Database=WinOasis;Trusted_Connection=yes;') 
     except pypyodbc.Error as ex: 
      sqlstate = ex.args[0] 
      if sqlstate == '28000': 
       print ("You do not have access.") 
     grab(servername.strip()) 

當我添加在底部的start()函數,這是行不通的。它會移至例外情況,說 數據庫關閉或者您沒有連接到網絡。

import os 
import shutil 
import fileinput 
import pypyodbc 

def replaceid(servername): 
    try: 
     cursor = connection.cursor() 

     SQLCommand = ("SELECT Name, Location_ID " 
      "FROM dbo.I_Location " # table name 
      "with (nolock)" 
      "WHERE Name = ?") 
     Values = [servername] 
     cursor.execute(SQLCommand,Values) 
     results = cursor.fetchone() 
     if results: 

      print (" Name: " + results[0] + " Location ID: " + str(results[1])) 
      print (" ") 
     else: 
      print (" Location ID for " + servername + " does not exist.") 
      print (" ") 
      connection.close() 
    except: 
     print("Database is down or you are not connected to network.") 
     exit() 

def grab(servername): 
# copy config from remote computer 

    source = r'//' + servername + '/c$/Administrator/' 
    dest = "." 
    file = "Admin.config" 
    if os.path.isfile(os.path.join(source, file)) 
     try: 
      shutil.copyfile(os.path.join(source, file), os.path.join(dest, file)) 

     except: 
      print (" Local directory you are copying to does not exist.") 
    else: 
     pass 

    replaceid(servername) 

def start(): 
    # Option 1 
    os.system('cls' if os.name == 'nt' else 'clear') 
    array = [] 
    with open("serverlist.txt", "r") as f: 
     for servername in f: 

      try: 
       connection = pypyodbc.connect('Driver={SQL Server};Server=mydbx;Database=WinOasis;Trusted_Connection=yes;') 
      except pypyodbc.Error as ex: 
       sqlstate = ex.args[0] 
       if sqlstate == '28000': 
        print ("You do not have access.") 
      grab(servername.strip()) 

start() 

什麼是造成這種情況的任何想法?

回答

5

當你把連接放到啓動函數裏面時,它變成了一個本地對象,而其他函數不能獲得連接!

如果他們使用相同的連接,您必須將連接作爲對象傳遞給每個函數!

grab(servername.strip(),connection) 
def grab(servername ,connection): 
def replaceid(servername,connection): 

改變這樣的,它應該是罰款(把搶函數內嘗試部分)