in progress: python scripting for database population and management
This commit is contained in:
5
db/util/categories.csv
Normal file
5
db/util/categories.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
name
|
||||
Spices
|
||||
Herbs
|
||||
Chili Peppers
|
||||
Salt & Pepper
|
||||
|
67
db/util/import_csv.py
Normal file
67
db/util/import_csv.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from logging.config import IDENTIFIER
|
||||
import psycopg2
|
||||
from psycopg2 import sql
|
||||
import csv
|
||||
import os
|
||||
|
||||
# read data from environment if present
|
||||
env_path = "../../.env"
|
||||
fd = os.open(env_path, os.O_RDONLY)
|
||||
n = 300
|
||||
|
||||
file_data = os.read(fd, n)
|
||||
parsed_string = ""
|
||||
|
||||
# convert each ASCII value to its corresponding character
|
||||
for c in file_data:
|
||||
parsed_string = parsed_string + chr(c)
|
||||
|
||||
start = parsed_string.find('postgres')
|
||||
end = parsed_string.find('EXPRESS')
|
||||
|
||||
# receive conn string from env
|
||||
constring = parsed_string[start:end]
|
||||
os.close(fd)
|
||||
|
||||
# connect to local database instance and open a cursor
|
||||
conn = psycopg2.connect("dbname=e-commerce-092122 user=mikayladobson")
|
||||
cur = conn.cursor()
|
||||
|
||||
# read and print data from selection
|
||||
cur.execute("SELECT * FROM users;")
|
||||
for i in cur.fetchall():
|
||||
print(i)
|
||||
|
||||
# function to read from a given csv file into postgres
|
||||
def insert_file_contents(file_path, table_name, column_count):
|
||||
with open(file_path, 'r') as f:
|
||||
reader = csv.reader(f)
|
||||
header_names = ""
|
||||
first_row_accessed = False
|
||||
|
||||
for row in reader:
|
||||
# get row values from first row of reader
|
||||
if not first_row_accessed:
|
||||
header_names = row
|
||||
first_row_accessed = True
|
||||
continue
|
||||
|
||||
# execute table insertion for each row of csv based on number of columns
|
||||
if column_count == 1:
|
||||
cur.execute(sql.SQL(
|
||||
"INSERT INTO {} (%s) VALUES (%s)").format(sql.Identifier(table_name)),
|
||||
[header_names, row]
|
||||
)
|
||||
elif column_count == 3:
|
||||
cur.execute(sql.SQL("INSERT INTO {} ({}) VALUES (%s, %s, %s)".format(sql.Identifier(table_name), sql.Identifier(header_names[0])), row))
|
||||
else:
|
||||
raise
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
insert_file_contents("./categories.csv", "category", 1)
|
||||
|
||||
cur.execute("SELECT * FROM category;")
|
||||
for i in cur.fetchall():
|
||||
print(i)
|
||||
21
db/util/main.py
Normal file
21
db/util/main.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import psycopg2
|
||||
import os
|
||||
|
||||
# read data from environment if present
|
||||
path = "../../.env"
|
||||
fd = os.open(path, os.O_RDONLY)
|
||||
n = 300
|
||||
|
||||
file_data = os.read(fd, n)
|
||||
parsed_string = ""
|
||||
|
||||
# convert each ASCII value to its corresponding character
|
||||
for c in file_data:
|
||||
parsed_string = parsed_string + chr(c)
|
||||
|
||||
start = parsed_string.find('postgres')
|
||||
end = parsed_string.find('EXPRESS')
|
||||
|
||||
# receive conn string from env
|
||||
constring = parsed_string[start:end]
|
||||
os.close(fd)
|
||||
51
db/util/products.csv
Normal file
51
db/util/products.csv
Normal file
@@ -0,0 +1,51 @@
|
||||
id,name,regionId,categoryId
|
||||
1,Allspice - Whole,8,1
|
||||
2,Cocoa Powder,1,1
|
||||
3,Cardamom - Green,3,1
|
||||
4,Cardamom - Black,4,1
|
||||
5,Cinnamon,2,1
|
||||
6,Cloves,1,1
|
||||
7,Fenugreek Seed,4,1
|
||||
8,Lavender Flowers,3,2
|
||||
9,Nutmeg,8,1
|
||||
10,Star Anise,2,1
|
||||
11,Vanilla Bean - Madagascar,1,1
|
||||
12,Vanilla Bean - Mexico,5,1
|
||||
13,Vanilla Paste,8,1
|
||||
14,Ancho Chile,5,3
|
||||
15,Cayenne - African,1,3
|
||||
16,Cayenne - Indian,4,3
|
||||
17,Chipotle Chile,5,3
|
||||
18,Ghost Chile,4,3
|
||||
19,Guajillo Chile,5,3
|
||||
20,Paprika - Hungarian,3,3
|
||||
21,Paprika - Spain,3,3
|
||||
22,Piment d'Esplette,3,3
|
||||
23,Thai Chile,2,3
|
||||
24,Basil,1,2
|
||||
25,Bay Leaf - California,7,2
|
||||
26,Bay Leaf - Turkish,3,2
|
||||
27,Chives,7,2
|
||||
28,Dill Weed,7,2
|
||||
29,Fenugreek Leaf,4,2
|
||||
30,Herbes de Provence,3,2
|
||||
31,Lemongrass,2,2
|
||||
32,Marjoram,1,2
|
||||
33,Oregano - Mexican,5,2
|
||||
34,Oregano - Turkish,3,2
|
||||
35,Parsley,7,2
|
||||
36,Peppermint,7,2
|
||||
37,Rosemary,3,2
|
||||
38,Sage,3,2
|
||||
39,Tarragon,3,2
|
||||
40,Fleur de Sel,3,4
|
||||
41,Grains of Paradise,1,4
|
||||
42,Himalayan Pink Salt,2,4
|
||||
43,Long Pepper,2,4
|
||||
44,Maldon Flake Sea Salt,3,4
|
||||
45,Peppercorn - Black,4,4
|
||||
46,Peppercorn - Green,4,4
|
||||
47,Peppercorn - Pink,8,4
|
||||
48,Peppercorn - Red,2,4
|
||||
49,Peppercorn - White,2,4
|
||||
50,Pepper - Szechuan,2,4
|
||||
|
9
db/util/regions.csv
Normal file
9
db/util/regions.csv
Normal file
@@ -0,0 +1,9 @@
|
||||
id,name
|
||||
1,Africa
|
||||
2,Asia
|
||||
3,Europe
|
||||
4,India
|
||||
5,Latin America
|
||||
6,Middle East
|
||||
7,North America
|
||||
8,Carribean
|
||||
|
Reference in New Issue
Block a user