Bot_Development / db /save_data.py
dsmultimedika's picture
Improve the code bot development
d57efd6
raw
history blame
1.29 kB
from databases import Database
import logging
from dotenv import load_dotenv
from db.repository import Repository
load_dotenv()
class InsertDatabase(Repository):
# Example function to insert data asynchronously
async def insert_data(self, params, category_id):
# SQL insert query with named placeholders
query = """
INSERT INTO metadata (title, category_id, author, year, publisher)
VALUES (:title, :category_id, :author, :year, :publisher)
"""
reference = {
"title": params["title"],
"category_id": category_id, # directly assign category_id
"author": params["author"],
"year": params["year"],
"publisher": params["publisher"]
}
print(reference)
try:
# Execute the query with the provided values
await self._exec(query, reference)
logging.info(
f"Data inserted successfully: {reference['title']}, {reference['author']}"
)
except Exception as e:
# Log any errors that occur during the database insert operation
logging.error(f"Failed to insert data: {e}")
raise # Re-raise the exception to allow further handling if needed