0

I'm working on building a python code that cleans and organizes data from an mdb database using pypyodbc. Everything was working fine until abruptly a previous function stopped working.

I norrowed the problem down to this function, and when i run the function on its own in a seperate file the exact same issue occurs. It seems as if the conn.close() is the source of the problem, it hangs at that line and then the program "finishes" with no errors.

import pypyodbc
import logging



# Logging

doDEBUG = True

if doDEBUG == True:
    logging.basicConfig(level=logging.DEBUG, filename="log.log", filemode="w", format="%(asctime)s - %(levelname)s - Line:%(lineno)d: %(message)s")

else:
    logging.basicConfig(level=logging.INFO, filename="log.log", filemode="w", format="%(asctime)s - %(levelname)s - Line:%(lineno)d: %(message)s")




# Constants

ACCDRIVER = "{Microsoft Access Driver (*.mdb, *.accdb)}"    # assigns driver to use to connect to source database file
ACCFILELOC = r"*file_path\file.mdb*"     # data-base file for where the input data will be pulled from




def getTablesInfo():

    # makes the connection to the source data file
    conn = pypyodbc.connect(f"Driver={ACCDRIVER};Dbq={ACCFILELOC};") 
    crsr = conn.cursor()


    # Finds table names in input data-base and groups them into a list
    tableList = []
    for table in crsr.tables():
        if table[3] == "TABLE":
            tableList.append([table[2]])
    
    
    # Finds the field names and their datatypes for each table
    for tableIndex, table in enumerate(tableList):
        
        fieldList = []
        
        for field in crsr.columns(table[0]):
            fieldList.append([field[3], field[5], field[6]*2]) # field name, field datatype, field length (database is rough so doubling length for later use)
            
        tableList[tableIndex].append(fieldList)
                
            
            
    # closes connection to cursor and closes connection to the database
    logging.debug("here2")
    
    crsr.close()
    logging.debug("here3")
    
    conn.close()
    logging.debug("here4")
    
    
    return(tableList)


logging.debug("here1")

tableList = getTablesInfo()

logging.debug("here5")

The log.log file after the program tries to run looks like this:

2023-05-30 17:41:25,593 - DEBUG - Line:68: here1
2023-05-30 17:41:25,846 - DEBUG - Line:56: here2
2023-05-30 17:41:25,846 - DEBUG - Line:59: here3

The log file confirms that it hangs at the conn.close() line.

This function was working perfectly fine before, then with no alteration it just stopped working. I've tried restarting vscode, restarting my computer, shutting my computer off, waiting, then tunring it back on. All with no change.

Any ideas what would cause a connection close to fail in the first place or what would cause it to work and then fail suddenly?

I've tried replacing the conn = pypyodbc.connect(f"Driver={ACCDRIVER};Dbq={ACCFILELOC};") with with pypyodbc.connect(f"Driver={ACCDRIVER};Dbq={ACCFILELOC};") as conn: but the program still hangs and fails in the same location.

Additionally removing all the code between the open and close statements still doesn't resolve the issue and it still hangs and gives up.

Rebooting seems to have no affect.

Running the program in the terminal instead of through vscode produces the same result.

Just to reiterate: The program gives no error, as far as I'm aware I believe the program thinks it's done everything right.

(I'm using python 3.11.3 and Windows 11 for work)

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Isaac
  • 1
  • 1
  • Have you tried checking if the connection is still there? You could add something like `if conn: conn.close()` to close the connection variable if it is still open. Or, you can use `with` to open the connection and cursor and they will handle of closing and disposing of objects. – S. MacKenzie May 31 '23 at 23:41

0 Answers0