deleted
This commit is contained in:
BIN
data.sqlite3
BIN
data.sqlite3
Binary file not shown.
5
form.py
5
form.py
@@ -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))
|
|
||||||
31
format.py
31
format.py
@@ -1,40 +1,43 @@
|
|||||||
#it is used to clean and create the database
|
#it is used to clean and create the database
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import pandas
|
import pandas
|
||||||
|
import numpy
|
||||||
conn = sqlite3.connect("data.sqlite3")
|
conn = sqlite3.connect("data.sqlite3")
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
df = pandas.read_csv("offencebymonth.csv")
|
df = pandas.read_csv("offencebymonth.csv")
|
||||||
#drop empty value
|
#drop empty value
|
||||||
new_df = df.dropna()
|
new_df = df.dropna()
|
||||||
#get rid of spaces
|
|
||||||
new_df.replace(' ', '_', regex=True)
|
|
||||||
LGA = df['LGA'].unique()
|
LGA = df['LGA'].unique()
|
||||||
subdiv = df['Statistical Division or Subdivision'].unique()
|
subdiv = df['Statistical Division or Subdivision'].unique()
|
||||||
cat = df["Offence category"].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
|
#database split to 3 tables, main statistics, subdiv index and statistics4
|
||||||
#Enale foreign key support
|
#Enale foreign key support
|
||||||
print(cat)
|
|
||||||
|
|
||||||
cur.execute("PRAGMA foreign_keys = ON;")
|
cur.execute("PRAGMA foreign_keys = ON;")
|
||||||
|
|
||||||
categories = """CREATE TABLE IF NOT EXISTS categories(id INTEGER AUTO INCREMENT PRIMARY KEY, name TEXT)"""
|
categories = """CREATE TABLE IF NOT EXISTS categories(id INTEGER PRIMARY KEY AUTOINCREMENT , 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))"""
|
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 AUTO INCREMENT PRIMARY KEY, subdiv TEXT, LGA TEXT)"""
|
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 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))"""
|
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(categories)
|
||||||
cur.execute(subcat)
|
cur.execute(subcat)
|
||||||
cur.execute(location)
|
cur.execute(location)
|
||||||
cur.execute(stat)
|
cur.execute(stat)
|
||||||
#insert statements
|
#insert statements
|
||||||
categories_ins = """INSERT INTO categories VALUES(NULL, ?)"""
|
categories_ins = """INSERT INTO categories VALUES(NULL, :category)"""
|
||||||
subcat_ins = """INSERT INTO subcat VALUES(NULL, ?, ?)"""
|
subcat_ins = """INSERT INTO subcat VALUES(NULL, :name, :catid)"""
|
||||||
location_ins = """INSERT INTO location(NULL, ?, ?)"""
|
location_ins = """INSERT INTO location(NULL, ?, ?)"""
|
||||||
stat_ins = """INSERT INTO statistics(NULL, ?,?,?,?,?)"""
|
stat_ins = """INSERT INTO statistics(NULL, ?,?,?,?,?)"""
|
||||||
cat = cat.tolist()
|
cat = cat.tolist()
|
||||||
print(cat)
|
for i in range(len(cat)):
|
||||||
for category in cat:
|
cur.execute(categories_ins, {"category": cat[i]})
|
||||||
print(category)
|
|
||||||
cur.executemany(categories_ins, category)
|
|
||||||
|
|
||||||
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())
|
print(cur.fetchall())
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user