Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3 #databasesystem of my choice
- import pathlib #to check if a file exists
- import traceback #for error handling
- def CreateConnection(databaseFilePath):
- """ create a database connection to the SQLite database
- specified by databaseFilePath
- :param databaseFilePath: database file path for example c:/someDatabase.db
- :return: Connection object or None
- """
- if not pathlib.Path(databaseFilePath).is_file():
- raise FileNotFoundError
- else:
- conn = None
- try:
- conn = sqlite3.connect(databaseFilePath)
- return conn
- except sqlite3.Error as e:
- traceback.print_exc()
- return conn
- def CloseConnectionToDatabase(databaseConnection): #still not sure if that should be renamed to CloseObject
- """
- closes a connection to the database specified in databaseConnection
- :param databaseConnection: should be a Connenction object
- """
- try:
- databaseConnection.close()
- except IOError as exc:
- traceback.print_exc()
- try:
- test=CreateConnection('./generated_files/test.db')
- except Exception as e:
- traceback.print_exc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement