This commit is contained in:
dchen4
2024-12-02 21:01:36 +11:00
parent d01081a37f
commit 2c90544601
6 changed files with 17 additions and 9634 deletions

View File

Binary file not shown.

0
db.js
View File

View File

@@ -1,5 +0,0 @@
import csv
with open("rci-offencebymonth.csv", newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
print(','.join(row))

View File

@@ -1,40 +1,43 @@
#it is used to clean and create the database
import sqlite3
import pandas
import numpy
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()
subcat_v = df[['Offence category', 'Subcategory']].drop_duplicates()
subcat_v = subcat_v.values.tolist()
#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))"""
categories = """CREATE TABLE IF NOT EXISTS categories(id INTEGER PRIMARY KEY AUTOINCREMENT , name TEXT)"""
subcat = """CREATE TABLE IF NOT EXISTS subcat(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, cat_id INTEGER, FOREIGN KEY(cat_id) REFERENCES categories(id))"""
location = """CREATE TABLE IF NOT EXISTS location(id INTEGER PRIMARY KEY AUTOINCREMENT , subdiv TEXT, LGA TEXT)"""
stat = """CREATE TABLE IF NOT EXISTS statistics(id INTEGER PRIMARY KEY AUTOINCREMENT , 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, ?, ?)"""
categories_ins = """INSERT INTO categories VALUES(NULL, :category)"""
subcat_ins = """INSERT INTO subcat VALUES(NULL, :name, :catid)"""
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)
for i in range(len(cat)):
cur.execute(categories_ins, {"category": cat[i]})
cur.execute("SELECT * FROM categories")
for i in range(len(subcat_v)):
cur.execute("SELECT id FROM categories WHERE name='"+subcat_v[i][0]+"';")
cur.execute(subcat_ins, {"name": subcat_v[i][1], "catid" : cur.fetchall()[0][0]})
cur.execute("SELECT * FROM subcat")
print(cur.fetchall())

File diff suppressed because it is too large Load Diff