database insertions function properly from python script

This commit is contained in:
Mikayla Dobson
2022-09-22 20:14:13 -05:00
parent 2e8a811659
commit eb18065135
5 changed files with 122 additions and 136 deletions

View File

@@ -20,7 +20,7 @@ async function main() {
const createCartTable = ` const createCartTable = `
CREATE TABLE IF NOT EXISTS cart ( CREATE TABLE IF NOT EXISTS cart (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL, id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
appUserId INT REFERENCES users(id), appUserId INT REFERENCES users(id)
); );
`; `;
@@ -49,7 +49,7 @@ async function main() {
CREATE TABLE IF NOT EXISTS category ( CREATE TABLE IF NOT EXISTS category (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL, id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
name VARCHAR NOT NULL, name VARCHAR NOT NULL,
description VARCHAR NOT NULL description VARCHAR
); );
`; `;
@@ -58,10 +58,10 @@ async function main() {
CREATE TABLE IF NOT EXISTS product ( CREATE TABLE IF NOT EXISTS product (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL, id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
name VARCHAR NOT NULL, name VARCHAR NOT NULL,
description VARCHAR NOT NULL, description VARCHAR,
categoryId INT REFERENCES category(id), categoryId INT REFERENCES category(id),
regionId INT REFERENCES region(id), regionId INT REFERENCES region(id),
price NUMERIC NOT NULL price NUMERIC
); );
`; `;
@@ -87,8 +87,8 @@ async function main() {
let status; let status;
try { try {
await client.query("DROP SCHEMA public CASCADE; CREATE SCHEMA public;"); await client.query("DROP SCHEMA public CASCADE; CREATE SCHEMA public");
for (let q of allQueries) { for (let q of allQueries) {
await client.query(q); await client.query(q);
} }
@@ -97,6 +97,10 @@ async function main() {
status = "Database setup successful!"; status = "Database setup successful!";
} catch(e) { } catch(e) {
status = e; status = e;
} finally {
if (status !== "Database setup successful!") {
throw new Error(status);
}
} }
console.log(status); console.log(status);

View File

@@ -1,67 +0,0 @@
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)

View File

@@ -1,9 +1,11 @@
import psycopg2 import psycopg2
from psycopg2 import sql
import csv
import os import os
# read data from environment if present # read data from environment if present
path = "../../.env" env_path = "../../.env"
fd = os.open(path, os.O_RDONLY) fd = os.open(env_path, os.O_RDONLY)
n = 300 n = 300
file_data = os.read(fd, n) file_data = os.read(fd, n)
@@ -18,4 +20,51 @@ end = parsed_string.find('EXPRESS')
# receive conn string from env # receive conn string from env
constring = parsed_string[start:end] constring = parsed_string[start:end]
os.close(fd) 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', encoding='utf-8-sig') 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 = [item for item in row]
first_row_accessed = True
continue
# execute table insertion for each row of csv based on number of columns
if column_count == 3:
cur.execute(sql.SQL("INSERT INTO {table} ({h1}, {h2}, {h3}) VALUES (%s, %s, %s)".format(
table=table_name,
h1=header_names[0],
h2=header_names[1],
h3=header_names[2]
)),
row
)
elif column_count == 1:
cur.execute(sql.SQL("INSERT INTO {table} ({field}) VALUES (%s)".format(table=table_name, field=header_names[0])), row)
else:
raise
conn.commit()
insert_file_contents("./categories.csv", 'category', 1)
insert_file_contents("./regions.csv", 'region', 1)
insert_file_contents("./products.csv", 'product', 3)
print("Database insertions executed successfully.")

View File

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

View File

@@ -1,9 +1,9 @@
id,name name
1,Africa Africa
2,Asia Asia
3,Europe Europe
4,India India
5,Latin America Latin America
6,Middle East Middle East
7,North America North America
8,Carribean 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