def sql(query, db: scoped_session = db, fetch=False, rollback=False): """ runs sql query on db """ if rollback: db.rollback() if fetch: return db.execute(query).fetchall() db.execute(query) db.commit()
def populate_books_table(db: scoped_session = db, data_path='books.csv'): # Good progress bars try: __IPYTHON__ from tqdm import tqdm_notebook as tqdm except NameError: from tqdm import tqdm as tqdm import pandas as pd if not table_exists('books', db=db): sql(connect_database.config['queries']['create books'], rollback=True, db=db) sql(connect_database.config['queries']['clear books'], rollback=True) data_path = Path(data_path).absolute() data = pd.read_csv(data_path) db.rollback() for i in tqdm(range(len(data))): book = data.iloc[i] isbn, title, author, year = escape_list(list(book)) # set_trace() query = f"INSERT INTO books(isbn, title, author, year)" + \ f""" VALUES ('{isbn}', '{title}', '{author}', '{year}')""" db.execute(query) db.commit() print("Successful")
def add_user(email: str, password: str, db: scoped_session): """ Adds a USER to the Database """ query = "" db.execute()