0

I'm new to Python. Only been coding a month. I am stumped. The below code works perfectly until I move it to a function. I'm not at work so I can't remember the exact error, but it was something about no temp registry access. The exact error is not important since the code is working. What I can't figure out is why I can't call this code from a function. I can import this code with 'from filename.py import *' and it works.

Can an experienced Python coder explain why the access connection will not work in a function call?

import pypyodbc

gage_list = []
gage=0
ser_num=1
owner=2
duedate=3
status=4

#create connection
DBfile = "C:/ProgramData/CyberMetrics Corporation/GAGEtrak 7.0/AppFiles/Gtw70.accdb"
conn = pypyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+DBfile)
cursor = conn.cursor()
cursor.execute("SELECT Gage_ID FROM Gage_Master")
Gage_ID=cursor.fetchall()

cursor.execute("SELECT Gage_SN FROM Gage_Master")
Gage_SN=cursor.fetchall()

cursor.execute("SELECT GM_Owner FROM Gage_Master")
GM_Owner=cursor.fetchall()

cursor.execute("SELECT Next_Due_Date FROM Gage_Master")
Next_Due_Date=cursor.fetchall()

cursor.execute("SELECT Status FROM Gage_Master")
Status=cursor.fetchall()

conn.close()
counter = 0


for i in range(len(Gage_ID)): #combines gage columns into an array
    tup = Gage_ID[i][0], Gage_SN[i][0], GM_Owner[i][0], Next_Due_Date[i][0], Status[i][0]
    gage_list.append(tup)

print(gage_list[14][duedate]) #to choose a field, select from gage, ser_num, owner, duedate, or status variables
Erik A
  • 31,639
  • 12
  • 42
  • 67
Eddie Snipes
  • 119
  • 1
  • 1
  • 8
  • 3
    Please can you show the function's code? – Nuno André Nov 23 '17 at 02:11
  • 2
    _"about no temp registry access"_ - without the exact error, and the function that you wrote encapsulating the code, I'm afraid you won't get much help. – Burhan Khalid Nov 23 '17 at 02:17
  • 3
    It might be better to post the non-working function and the code that calls it. – mhawke Nov 23 '17 at 02:18
  • The only thing different between this code and what doesn't work is that I created the following: def importAccessDB(): .... the above code indented here importAccessDB() The only thing I'm trying to do is keep the code organized. I have other functions that use the variables from this code. – Eddie Snipes Nov 23 '17 at 02:26
  • I'm wondering if this has something to do with global / local variables. Though I can't see why. I tried declaring the variables outside the function, and inside. I even tried global variableNames to see if it made a difference. – Eddie Snipes Nov 23 '17 at 02:31
  • If you initialize the variables inside a function, they won't be accessible outside of the function. Try initializing the ones you use elsewhere outside and before the definition of `importAccessDB`. Edit: just saw you tried putting them outside already. –  Nov 23 '17 at 02:33
  • My problem is that it won't even read the database inside the function. Once I remove the def and run it, everything works. – Eddie Snipes Nov 23 '17 at 16:55
  • I'll repost this question with more details on Monday when I can get the exact error. – Eddie Snipes Nov 23 '17 at 16:55
  • I was able to get this working. I believe it was a global / local variable issue. I appreciate all the tips. – Eddie Snipes Dec 05 '17 at 02:40

0 Answers0