-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_db.py
123 lines (101 loc) · 4.99 KB
/
create_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
116
117
118
119
120
121
122
123
"""
This module will create the database and tables for the city_data.db file.
It is only run is the city_data.db file does not exist.
Functions:
main: Creates the database and tables and populates the tables with data from JSON files.
"""
import sys
import os
from database.database_functions.read_json import read_json
from database.database_functions.get_file_path import get_file_path
from database.database_functions.create_table import create_table
from database.database_functions.create_connection import create_connection
from database.database_functions.insert_us_cities import insert_us_cities
from database.database_functions.insert_world_cities import insert_world_cities
from database.database_functions.insert_growth import insert_growth
from database.database_functions.insert_metro_systems import insert_metro_systems
from database.database_functions.create_us_city_class_table import create_us_city_class_table, insert_us_city_class_data
from database.database_functions.create_world_city_class_table import create_world_city_class_table, insert_world_city_class_data
# add the parent directory to the path so we can import the file_functions module
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
def main():
"""
Main function to create the database and tables
"""
database = get_file_path("city_data.db")
# create the tables
print('Creating the us_cities table...')
us_city_table = """CREATE TABLE IF NOT EXISTS us_cities (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT NOT NULL,
state TEXT NOT NULL,
countryCode TEXT NOT NULL,
latitude REAL NOT NULL,
longitude REAL NOT NULL,
population INTEGER NOT NULL,
area REAL NOT NULL
);"""
print('Creating the world_cities table...')
world_city_table = """CREATE TABLE IF NOT EXISTS world_cities (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
name2 TEXT,
country TEXT NOT NULL,
countryCode TEXT NOT NULL,
latitude REAL NOT NULL,
longitude REAL NOT NULL,
population INTEGER NOT NULL,
area REAL NOT NULL
);"""
print('Creating the metro_systems table...')
metro_table = """CREATE TABLE IF NOT EXISTS metro_systems (
id INTEGER PRIMARY KEY,
city TEXT NOT NULL,
country_region TEXT NOT NULL,
name TEXT NOT NULL,
service_opened INTEGER NOT NULL,
last_expanded INTEGER NOT NULL,
stations INTEGER NOT NULL,
system_length REAL NOT NULL,
annual_ridership REAL,
rail_type TEXT NOT NULL
);"""
print('Creating the growth table...')
growth_table = """CREATE TABLE IF NOT EXISTS growth (
id INTEGER PRIMARY KEY,
pop2023 INTEGER NOT NULL,
pop2022 INTEGER NOT NULL,
city TEXT NOT NULL,
country TEXT NOT NULL,
growthRate REAL NOT NULL,
type TEXT NOT NULL,
rank INTEGER NOT NULL,
state TEXT
);"""
conn = create_connection(database)
if conn is not None:
create_table(conn, us_city_table)
create_table(conn, world_city_table)
create_table(conn, metro_table)
create_table(conn, growth_table)
# Read the JSON files and import the data into the tables
us_cities_data = read_json(get_file_path("usCityList.json"))
insert_us_cities(conn, us_cities_data)
world_cities_data = read_json(get_file_path("worldCityList.json"))
insert_world_cities(conn, world_cities_data)
metro_systems_data = read_json(get_file_path("metroList.json"))
insert_metro_systems(conn, metro_systems_data)
growth_data = read_json(get_file_path("growth.json"))
insert_growth(conn, growth_data)
# Create the us_city_class table and insert the data
create_us_city_class_table(conn)
insert_us_city_class_data(conn)
create_world_city_class_table(conn)
insert_world_city_class_data(conn)
print()
conn.commit()
else:
print("Error! Cannot create the database connection.")
if __name__ == "__main__":
main()