-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathingest_data.py
72 lines (58 loc) · 2.11 KB
/
ingest_data.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
import psycopg2
class DatabaseManager:
def __init__(self, host, port, dbname, user, password):
self.conn_string = (
f"host={host} port={port} dbname={dbname} user={user} password={password}"
)
self.conn = None
self.cursor = None
def connect(self):
self.conn = psycopg2.connect(self.conn_string)
self.cursor = self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def setup_database(self):
self.connect()
# Create a new table for products
create_table_query = """
CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
name VARCHAR(100) UNIQUE,
price DECIMAL(10, 2),
description TEXT,
category VARCHAR(100)
);
"""
self.cursor.execute(create_table_query)
self.conn.commit()
self.close()
def insert_food_items(self, file_path):
self.connect()
# Read data from the provided file
with open(file_path, "r") as file:
food_items = file.readlines()
# Insert each food item into the database
for line in food_items:
name, price, description, category = line.strip().split("; ")
price = price.replace("$", "") # Remove the dollar sign
insert_query = """
INSERT INTO products (name, price, description, category)
VALUES (%s, %s, %s, %s)
ON CONFLICT (name) DO NOTHING;
"""
self.cursor.execute(insert_query, (name, price, description, category))
self.conn.commit()
self.close()
def query_and_print(self):
self.connect()
self.cursor.execute("SELECT * FROM products;")
products = self.cursor.fetchall()
for product in products:
print(product)
self.close()
if __name__ == "__main__":
db_manager = DatabaseManager("localhost", "5432", "vectordb", "admin", "admin")
db_manager.setup_database()
db_manager.insert_food_items("./data/food.txt")
db_manager.query_and_print()