def insert_category(self, category_list): """insert data in table category""" cursor = db.cursor() try: for i, _value in enumerate(category_list): query = (f"""INSERT INTO {self.table_name} (name) VALUES ("{category_list[i].name}") """) data = category_list[i] cursor.execute(query, data) db.commit() cursor.close() print(f'Data successfully inserted in table: {self.table_name}') except Error as err: print(f'Failed to insert data in MySQL table: {err}')
def insert_productbrand(self, prodstore_list): """ insert data in association table productbrand""" cursor = db.cursor() try: for i, _value in enumerate(prodstore_list): query = (f"""INSERT INTO {self.table_name} (product_idProduct, brand_idBrand) VALUES (%s, %s)""") data = prodstore_list[i] cursor.execute(query, data) db.commit() cursor.close() print(f'Data successfully inserted in table: {self.table_name}') except Error as err: print(f'Failed to insert data in MySQL table: {err}')
def save_substitute(self, product_origin, product_substitute): """ save product origin and product substitute in database """ date_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') cursor = db.cursor() try: query = (f"""INSERT INTO {self.table_name} (idProductOrigin, idProductSubstitute, requestDate) VALUES (%s, %s, %s)""") data = (product_origin.idProduct, product_substitute.idProduct, date_time) cursor.execute(query, data) db.commit() cursor.close() except Error as err: print(f'Failed to insert data in MySQL table: {err}')
def insert_product(self, product_list): """ insert product in product table""" cursor = db.cursor() try: for i, _value in enumerate(product_list): query = (f""" INSERT INTO {self.table_name} (name, description, nutriscore, url) VALUES ("{product_list[i].name}", "{product_list[i].description}", "{product_list[i].nutriscore}", "{product_list[i].url}") """) data = product_list[i] cursor.execute(query, data) db.commit() cursor.close() print(f'Data successfully inserted in table: {self.table_name}') except Error as err: print(f'Failed to insert data in MySQL table: {err}')