add py script

This commit is contained in:
Yurun Chen
2024-12-02 20:23:50 +11:00
parent 67f2ace46e
commit b7970e51d6
3 changed files with 9651 additions and 0 deletions

BIN
data.sqlite3 Normal file

Binary file not shown.

40
format.py Normal file
View File

@@ -0,0 +1,40 @@
#it is used to clean and create the database
import sqlite3
import pandas
conn = sqlite3.connect("data.sqlite3")
cur = conn.cursor()
df = pandas.read_csv("offencebymonth.csv")
#drop empty value
new_df = df.dropna()
#get rid of spaces
new_df.replace(' ', '_', regex=True)
LGA = df['LGA'].unique()
subdiv = df['Statistical Division or Subdivision'].unique()
cat = df["Offence category"].unique()
#database split to 3 tables, main statistics, subdiv index and statistics4
#Enale foreign key support
print(cat)
cur.execute("PRAGMA foreign_keys = ON;")
categories = """CREATE TABLE IF NOT EXISTS categories(id INTEGER AUTO INCREMENT PRIMARY KEY, name TEXT)"""
subcat = """CREATE TABLE IF NOT EXISTS subcat(id INTEGER AUTO INCREMENT PRIMARY KEY, name TEXT, cat_id INTEGER, FOREIGN KEY(cat_id) REFERENCES categories(id))"""
location = """CREATE TABLE IF NOT EXISTS location(id INTEGER AUTO INCREMENT PRIMARY KEY, subdiv TEXT, LGA TEXT)"""
stat = """CREATE TABLE IF NOT EXISTS statistics(id INTEGER AUTO INCREMENT PRIMARY KEY, category_id INTEGER, subcat_id INTEGER, location_id INTEGER, month TEXT NOT NULL, count INTEGER NOT NULL,FOREIGN KEY(category_id) REFERENCES categories(id), FOREIGN KEY(subcat_id) REFERENCES subcat(id), FOREIGN KEY(location_id) REFERENCES location(id))"""
cur.execute(categories)
cur.execute(subcat)
cur.execute(location)
cur.execute(stat)
#insert statements
categories_ins = """INSERT INTO categories VALUES(NULL, ?)"""
subcat_ins = """INSERT INTO subcat VALUES(NULL, ?, ?)"""
location_ins = """INSERT INTO location(NULL, ?, ?)"""
stat_ins = """INSERT INTO statistics(NULL, ?,?,?,?,?)"""
cat = cat.tolist()
print(cat)
for category in cat:
print(category)
cur.executemany(categories_ins, category)
cur.execute("SELECT * FROM categories")
print(cur.fetchall())

9611
offencebymonth.csv Normal file

File diff suppressed because it is too large Load Diff