-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathsql_db.py
115 lines (94 loc) · 3.19 KB
/
sql_db.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# sql_db.py
import sqlite3
from sqlite3 import Error
import random
from datetime import date, timedelta
from tqdm import tqdm
import pandas as pd
DATABASE_NAME = "mydatabase.db"
def create_connection():
""" Create or connect to an SQLite database """
conn = None;
try:
conn = sqlite3.connect(DATABASE_NAME)
except Error as e:
print(e)
return conn
def create_table(conn, create_table_sql):
""" Create a table with the specified SQL command """
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
def insert_data(conn, table_name, data_dict):
""" Insert a new data into a table """
columns = ', '.join(data_dict.keys())
placeholders = ', '.join('?' * len(data_dict))
sql = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})"
cur = conn.cursor()
cur.execute(sql, list(data_dict.values()))
conn.commit()
return cur.lastrowid
def query_database(query):
""" Run SQL query and return results in a dataframe """
conn = create_connection()
df = pd.read_sql_query(query, conn)
conn.close()
return df
# Create a financial table
def setup_financial_table():
conn = create_connection()
sql_create_financial_table = """
CREATE TABLE IF NOT EXISTS finances (
id INTEGER PRIMARY KEY,
date TEXT NOT NULL,
revenue REAL NOT NULL,
expenses REAL NOT NULL,
profit REAL NOT NULL
);
"""
create_table(conn, sql_create_financial_table)
# Insert 100 rows with random data
start_date = date.today() - timedelta(days=99)
for i in range(100):
revenue = random.randint(5000, 20000) # Random revenue between 5000 and 20000
expenses = random.randint(1000, 15000) # Random expenses between 1000 and 15000
profit = revenue - expenses
data = {
"date": start_date + timedelta(days=i),
"revenue": revenue,
"expenses": expenses,
"profit": profit
}
insert_data(conn, "finances", data)
conn.close()
def get_schema_representation():
""" Get the database schema in a JSON-like format """
conn = create_connection()
cursor = conn.cursor()
# Query to get all table names
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
db_schema = {}
for table in tables:
table_name = table[0]
# Query to get column details for each table
cursor.execute(f"PRAGMA table_info({table_name});")
columns = cursor.fetchall()
column_details = {}
for column in columns:
column_name = column[1]
column_type = column[2]
column_details[column_name] = column_type
db_schema[table_name] = column_details
conn.close()
return db_schema
# This will create the table and insert 100 rows when you run sql_db.py
if __name__ == "__main__":
# Setting up the financial table
# setup_financial_table()
# Querying the database
# print(query_database("SELECT * FROM finances"))
# Getting the schema representation
print(get_schema_representation())