in progress: python scripting for database population and management

This commit is contained in:
Mikayla Dobson
2022-09-22 18:19:59 -05:00
parent bf15b8871d
commit 2e8a811659
5 changed files with 153 additions and 0 deletions

5
db/util/categories.csv Normal file
View File

@@ -0,0 +1,5 @@
name
Spices
Herbs
Chili Peppers
Salt & Pepper
1 name
2 Spices
3 Herbs
4 Chili Peppers
5 Salt & Pepper

67
db/util/import_csv.py Normal file
View 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
View 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
View 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
1 id name regionId categoryId
2 1 Allspice - Whole 8 1
3 2 Cocoa Powder 1 1
4 3 Cardamom - Green 3 1
5 4 Cardamom - Black 4 1
6 5 Cinnamon 2 1
7 6 Cloves 1 1
8 7 Fenugreek Seed 4 1
9 8 Lavender Flowers 3 2
10 9 Nutmeg 8 1
11 10 Star Anise 2 1
12 11 Vanilla Bean - Madagascar 1 1
13 12 Vanilla Bean - Mexico 5 1
14 13 Vanilla Paste 8 1
15 14 Ancho Chile 5 3
16 15 Cayenne - African 1 3
17 16 Cayenne - Indian 4 3
18 17 Chipotle Chile 5 3
19 18 Ghost Chile 4 3
20 19 Guajillo Chile 5 3
21 20 Paprika - Hungarian 3 3
22 21 Paprika - Spain 3 3
23 22 Piment d'Esplette 3 3
24 23 Thai Chile 2 3
25 24 Basil 1 2
26 25 Bay Leaf - California 7 2
27 26 Bay Leaf - Turkish 3 2
28 27 Chives 7 2
29 28 Dill Weed 7 2
30 29 Fenugreek Leaf 4 2
31 30 Herbes de Provence 3 2
32 31 Lemongrass 2 2
33 32 Marjoram 1 2
34 33 Oregano - Mexican 5 2
35 34 Oregano - Turkish 3 2
36 35 Parsley 7 2
37 36 Peppermint 7 2
38 37 Rosemary 3 2
39 38 Sage 3 2
40 39 Tarragon 3 2
41 40 Fleur de Sel 3 4
42 41 Grains of Paradise 1 4
43 42 Himalayan Pink Salt 2 4
44 43 Long Pepper 2 4
45 44 Maldon Flake Sea Salt 3 4
46 45 Peppercorn - Black 4 4
47 46 Peppercorn - Green 4 4
48 47 Peppercorn - Pink 8 4
49 48 Peppercorn - Red 2 4
50 49 Peppercorn - White 2 4
51 50 Pepper - Szechuan 2 4

9
db/util/regions.csv Normal file
View 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
1 id name
2 1 Africa
3 2 Asia
4 3 Europe
5 4 India
6 5 Latin America
7 6 Middle East
8 7 North America
9 8 Carribean