Advertisement
Guest User

Untitled

a guest
Aug 31st, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import sqlite3 #databasesystem of my choice
  2. import pathlib #to check if a file exists
  3. import traceback #for error handling
  4.  
  5. def CreateConnection(databaseFilePath):
  6.     """ create a database connection to the SQLite database
  7.        specified by databaseFilePath
  8.    :param databaseFilePath: database file path for example c:/someDatabase.db
  9.    :return: Connection object or None
  10.    """
  11.  
  12.     if not pathlib.Path(databaseFilePath).is_file():
  13.         raise FileNotFoundError
  14.     else:
  15.         conn = None
  16.         try:
  17.             conn = sqlite3.connect(databaseFilePath)
  18.             return conn
  19.         except sqlite3.Error as e:
  20.             traceback.print_exc()
  21.         return conn
  22.  
  23. def CloseConnectionToDatabase(databaseConnection): #still not sure if that should be renamed to CloseObject
  24.     """
  25.    closes a connection to the database specified in databaseConnection
  26.    :param databaseConnection: should be a Connenction object
  27.    """
  28.     try:
  29.         databaseConnection.close()
  30.     except IOError as exc:
  31.         traceback.print_exc()
  32.  
  33.  
  34. try:
  35.     test=CreateConnection('./generated_files/test.db')
  36. except Exception as e:
  37.     traceback.print_exc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement