def create_categories(self, categorie_to_fill): ''' Function used to create the categorie into the DB depends of the categories we fill into settings ''' cursor = DB.cursor() sql = f"""INSERT INTO categorie (name) VALUES ('{categorie_to_fill}'); """ try: cursor.execute(sql) DB.commit() return True except Exception: DB.rollback()
def reset_bdd(self): ''' Function used to delete all tables ''' print("(Ré)initialisation en cours ...") with DB.cursor() as cursor: sql = '' with open(PATH_DB_SCRIPT, 'r') as sql_file: for line in sql_file: sql += line sql = sql.split(";") for line in sql: try: if line != "": cursor.execute(line) DB.commit() except Exception as e: print(f"Error reading the sql script {e}") DB.rollback()
def save_results_subst(self, id_old, id_subst): ''' Function used to write in DB the substitutions products ''' cursor = DB.cursor() sql = f""" INSERT IGNORE INTO substitut (id_initial_product, id_substitute_product) VALUES ('{id_old}' , '{id_subst}'); """ try: cursor.execute(sql) DB.commit() except Exception: DB.rollback() return False return True
def create_products(self, values, cat_id): ''' Categorie use to fill the product table Schema is: db_aliments : categorie_id, alim_name, store, website_link, nutriscore ''' name, nutriscore = values[0], values[1][0], store, link = values[2], values[3] cursor = DB.cursor() sql = f""" INSERT INTO db_aliments (categorie_id, alim_name, store, website_link, nutriscore) VALUES ({cat_id} , '{name}', '{store}', '{link}', '{nutriscore}'); """ try: cursor.execute(sql) DB.commit() return True except Exception: DB.rollback()