2017-08-04 76 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): 

改变这样的,它应该是罚款(把抢函数内尝试部分)