Created database creation scipts
This commit is contained in:
parent
ce24d792cf
commit
e04de06463
24
scripts/create_database.py
Normal file
24
scripts/create_database.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import sqlite3
|
||||||
|
from sqlite3 import Error
|
||||||
|
|
||||||
|
|
||||||
|
def create_connection(db_file):
|
||||||
|
""" create a database connection to a SQLite database """
|
||||||
|
conn = None
|
||||||
|
try:
|
||||||
|
conn = sqlite3.connect(db_file)
|
||||||
|
|
||||||
|
# Execute creation scripts
|
||||||
|
sql = open("create_tables.sql", "r");
|
||||||
|
conn.executescript(sql.read())
|
||||||
|
|
||||||
|
print(sqlite3.version)
|
||||||
|
except Error as e:
|
||||||
|
print(e)
|
||||||
|
finally:
|
||||||
|
if conn:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
create_connection(r"../data/wmgzon.db")
|
39
scripts/create_tables.sql
Normal file
39
scripts/create_tables.sql
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS Users (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
first_name TEXT NOT NULL,
|
||||||
|
last_name TEXT NOT NULL,
|
||||||
|
email TEXT NOT NULL UNIQUE,
|
||||||
|
phone TEXT NOT NULL UNIQUE,
|
||||||
|
password TEXT NOT NULL,
|
||||||
|
role TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
-- INSERT INTO Users (first_name, last_name, email, phone, password) VALUES ("Luke", "Else", "test@test.com", "07498 289321", "test213");
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS Products (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
cost DECIMAL NOT NULL,
|
||||||
|
sellerID INTEGER NOT NULL
|
||||||
|
REFERENCES Users (id)
|
||||||
|
ON DELETE CASCADE
|
||||||
|
ON UPDATE NO ACTION,
|
||||||
|
category TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS Orders (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
sellerID TEXT NOT NULL
|
||||||
|
REFERENCES Users (id)
|
||||||
|
ON DELETE NO ACTION
|
||||||
|
ON UPDATE NO ACTION,
|
||||||
|
total DECIMAL NOT NULL,
|
||||||
|
buyerID INTEGER NOT NULL
|
||||||
|
REFERENCES Users (id)
|
||||||
|
ON DELETE CASCADE
|
||||||
|
ON UPDATE NO ACTION,
|
||||||
|
orderDate DATE NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user