def setup_database(): """ Connect to the PostgreSQL database server """ conn = None try: print('Connecting to the PostgreSQL database...') conn = psycopg2.connect(host="db", port=5432, database="orders", user="******", password="******") cur = conn.cursor() query = """ DROP TABLE IF EXISTS order_item CASCADE; DROP TABLE IF EXISTS received_order CASCADE; DROP TABLE IF EXISTS menu_item CASCADE; """ print(query) cur.execute(query) conn.commit() cur.close() except (Exception, psycopg2.DatabaseError) as error: print(error) return False finally: if conn is not None: conn.close() print('Database connection closed.') # In case of exceptions, use the sqlalchmey api. db.drop_all() db.create_all() return True
def setUp(self): db.create_all() skello = Product() skello.name = "Skello" socialive = Product() socialive.name = "Socialive.tv" db.session.add(skello) db.session.add(socialive) db.session.commit()
from wsgi import db from wsgi import Student import os db.create_all() # Data to initialize database with Data = [{ 'name': 'Darren', 'physics': 1, 'maths': 1, 'chemistry': 1 }, { 'name': 'Kent', 'physics': 1, 'maths': 1, 'chemistry': 1 }, { 'name': 'Bunny', 'physics': 1, 'maths': 1, 'chemistry': 1 }] # Iterate over the PEOPLE structure and populate the database for xData in Data: d = Student(name=xData['name'], physics=xData['physics'], maths=xData['maths'], chemistry=xData['chemistry']) db.session.add(d)