-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_seed.py
55 lines (44 loc) · 1.56 KB
/
db_seed.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
import sqlite3
import json
try:
# Establish connection to the SQLite database
conn = sqlite3.connect('property_listings.db')
cursor = conn.cursor()
# Read property data from JSON file
with open('default_property_listings.json', 'r') as file:
property_data = json.load(file)
# Create a property table if it doesn't exist
cursor.execute("""
CREATE TABLE IF NOT EXISTS properties (
id INTEGER PRIMARY KEY,
location TEXT,
bedrooms INTEGER,
bathrooms INTEGER,
price REAL
)
""")
# Create a viewings table if it doesn't exist
cursor.execute("""
CREATE TABLE IF NOT EXISTS viewings (
id INTEGER PRIMARY KEY,
location TEXT,
viewing_date TEXT
)
""")
# Insert the property data into the database
for prop in property_data:
location = prop['location']
bedrooms = prop['bedrooms']
bathrooms = prop['bathrooms']
price = prop['price']
cursor.execute("INSERT INTO properties (location, bedrooms, bathrooms, price) VALUES (?,?,?,?)", (location, bedrooms, bathrooms, price))
# Commit the changes
conn.commit()
except sqlite3.Error as e:
print("SQLite error:", e)
except json.JSONDecodeError as e:
print("JSON decode error:", e)
finally:
# Close the connection in a finally block
if conn:
conn.close()