-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb01_setup.py
More file actions
72 lines (58 loc) · 2.04 KB
/
db01_setup.py
File metadata and controls
72 lines (58 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sqlite3
import pandas as pd
import pathlib
import logging
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
# Define database path
db_file = "project.sqlite3"
# Define the folder containing CSV files
data_folder = pathlib.Path("data")
# Define the folder containing SQL files
sql_folder = pathlib.Path("sql_create")
sql_files = [
sql_folder / "01_drop_tables.sql",
sql_folder / "02_create_tables.sql",
sql_folder / "03_insert_records.sql"
]
def create_database():
"""Creates the SQLite database file if it doesn't exist."""
try:
conn = sqlite3.connect(db_file)
conn.close()
logging.info(f"Database '{db_file}' created successfully.")
except sqlite3.Error as e:
logging.error(f"SQLite error while creating database: {e}")
def execute_sql_file(cursor, filename):
""" Reads an SQL file and executes its commands. """
sql_path = sql_folder / filename.name
if sql_path.exists():
with open(sql_path, 'r', encoding='utf-8') as file:
sql_script = file.read()
cursor.executescript(sql_script)
logging.info(f"Executed SQL file: {filename}")
else:
logging.warning(f"Warning: {filename} not found in {data_folder}")
def setup_database():
""" Connects to SQLite, executes SQL files, and sets up the database. """
try:
create_database()
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
logging.info("Connected to database successfully.")
# Execute SQL files in order
for sql_file in sql_files:
logging.info(f"Executing {sql_file}...")
execute_sql_file(cursor, sql_file)
conn.commit()
logging.info("Database setup completed successfully.")
except sqlite3.Error as e:
logging.error(f"SQLite error: {e}")
finally:
conn.close()
logging.info("Database connection closed.")
# Main function to run the setup
def main():
setup_database()
if __name__ == "__main__":
main()