WMGZON/scripts/create_database.py

37 lines
970 B
Python
Raw Normal View History

2024-01-01 16:20:44 +00:00
import sqlite3
import os
2024-01-01 16:20:44 +00:00
def create_connection(path: str, filename: str):
2024-01-01 16:20:44 +00:00
""" create a database connection to a SQLite database """
conn = None
try:
# Make the directory for the file to go into
create_directory(path)
print("Opening Database file and ensuring table integrity")
conn = sqlite3.connect(path + filename)
2024-01-01 16:20:44 +00:00
print("Database file open")
2024-01-01 16:20:44 +00:00
# Execute creation scripts
sql = open("scripts/create_tables.sql", "r");
2024-01-01 16:20:44 +00:00
conn.executescript(sql.read())
print("SQLite Version: " + sqlite3.version)
print("Table creation complete")
except sqlite3.Error as e:
2024-01-01 16:20:44 +00:00
print(e)
finally:
if conn:
conn.close()
# Ensure a directory is created given a path to it
def create_directory(dir: str):
try:
os.makedirs(dir)
except FileExistsError:
pass
2024-01-01 16:20:44 +00:00
if __name__ == '__main__':
create_connection(r"./data/", r"wmgzon.db")