Skip to content

Commit

Permalink
Update extract.py with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hrittikhere committed Jun 26, 2023
1 parent 557808a commit 452cf5d
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions mock-data/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
# CSV file path
csv_file = 'MOCK_DATA.csv'

# read csv file


# Read the CSV file
def read_csv(csv_file):
"""Read the CSV file"""
with open(csv_file, 'r') as f:
reader = csv.reader(f)
next(reader) # skip header
next(reader) # Skip header
data = list(reader)
return data

# connect to database


# Connect to the database
def get_db_connection():
conn = psycopg2.connect(
host=os.environ['POSTGRES_HOST'],
Expand All @@ -29,20 +25,16 @@ def get_db_connection():
print("Connection to PostgreSQL successful!")
return conn


conn = get_db_connection()
cur = conn.cursor()

# create table


# Create table
def create_table():
cur.execute("CREATE TABLE IF NOT EXISTS orders (order_id integer, customer_name text, customer_email text, customer_address text, product_name text, quantity integer, order_date date, priority text)")
print("Table created successfully")
conn.commit()


# write to database
# Write data to the database
def write_to_db():
data = read_csv(csv_file)
for user in data:
Expand All @@ -54,13 +46,14 @@ def write_to_db():
product = user[4]
quantity = user[5]
date = user[6]
priorty = user[7]
cur.execute("INSERT INTO orders (order_id, customer_name, customer_email, customer_address , product_name, quantity, order_date, priority) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
(id, first_name, email, address, product, quantity, date, priorty))

priority = user[7]
cur.execute("INSERT INTO orders (order_id, customer_name, customer_email, customer_address, product_name, quantity, order_date, priority) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
(id, first_name, email, address, product, quantity, date, priority))

# Create the table and write data to the database
create_table()
write_to_db()

# Commit changes and close the connection
conn.commit()
conn.close()

0 comments on commit 452cf5d

Please sign in to comment.