-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_growth.py
37 lines (29 loc) · 1.15 KB
/
insert_growth.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
"""
This module contains the function to insert data into the growth table of
the database
Parameters:
conn (sqlite3.Connection): A connection to the database
"""
import sqlite3
def insert_growth(conn, growth_data):
"""
Insert data into the growth table of the database
"""
query = """INSERT INTO growth (
id, pop2023, pop2022, city, country, growthRate, type, rank, state
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"""
# convert the list of dictionaries to a list of tuples
growth_tuples = []
for city in growth_data:
city_tuple = (city['id'], city['pop2023'], city['pop2022'], city['city'], city['country'], city['growthRate'],
city['type'], city['rank'], city['state'])
growth_tuples.append(city_tuple)
cursor = conn.cursor()
# insert the data into the database and check for duplicates
for city_tuple in growth_tuples:
try:
cursor.execute(query, city_tuple)
except sqlite3.IntegrityError:
print(f'Duplicate id found: Growth: {city_tuple[0]}, {city_tuple[3]}')
continue
conn.commit()