-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed_db.py
62 lines (51 loc) · 1.3 KB
/
seed_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
import psycopg2
# Database connection parameters
dbname = "my_database"
user = "user"
password = "password"
host = "localhost"
port = "5433" # Make sure this matches the port you've exposed in Docker
# SQL query to create a table
create_table_query = '''
CREATE TABLE IF NOT EXISTS web_data (
url VARCHAR(255),
title TEXT,
body TEXT,
comments TEXT
)
'''
create_table_query_rightmove = '''
CREATE TABLE IF NOT EXISTS rightmove (
price NUMERIC,
property_type TEXT,
bedrooms NUMERIC
)
'''
# Connect to PostgreSQL and create the table
try:
# Connect to the database
conn = psycopg2.connect(
dbname=dbname,
user=user,
password=password,
host=host,
port=port
)
conn.autocommit = True
cur = conn.cursor()
# Create the table
cur.execute(create_table_query)
print("web_data Table created successfully")
# Close the cursor and connection
conn.commit()
# Create the table
cur.execute(create_table_query_rightmove)
print("rightmove Table created successfully")
# Close the cursor and connection
conn.commit()
cur.execute("SELECT * FROM web_data;")
print("Table exists and can be queried.")
cur.close()
conn.close()
except Exception as e:
print(f"An error occurred: {e}")