-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdb_table_creation.py
More file actions
executable file
·29 lines (20 loc) · 984 Bytes
/
db_table_creation.py
File metadata and controls
executable file
·29 lines (20 loc) · 984 Bytes
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
from db.database_connection import db_connection, db
recipe_table = "CREATE TABLE recipes (id serial PRIMARY KEY , name VARCHAR(100), " \
"pre_time INTEGER CHECK ( pre_time > 0), difficulty INTEGER check ( difficulty > 0)," \
" vegetarian BOOLEAN, created_at TIMESTAMP )"
# db.execute("CREATE TABLE vubon (id serial PRIMARY KEY, num integer, data varchar);")
recipe_rating = "CREATE TABLE recipe_rating (recipe_id INTEGER REFERENCES recipes, rated INTEGER CHECK( rated > 0))"
db.execute("select exists(select * from information_schema.tables where table_name=%s)", ('recipes',))
# checking if table already exists then pass or create that table
if db.fetchone()[0]:
pass
else:
db.execute(recipe_table)
db.execute("select exists(select * from information_schema.tables where table_name=%s)", ('recipe_rating',))
if db.fetchone()[0]:
pass
else:
db.execute(recipe_rating)
db_connection.commit()
# db_connection.close()
# db.close()