Spaces:
Runtime error
Runtime error
Download db/save_data.py from Multimedika/Bot_Development: direct link, hf CLI and curl.
- Browser
- Download file 1.29 kB
-
https://huggingface.co/spaces/Multimedika/Bot_Development/resolve/a0b918a749e8a598e36c4e05672bd60a10caff43/db/save_data.py
- Command line
-
hf download hf://spaces/Multimedika/Bot_Development@a0b918a749e8a598e36c4e05672bd60a10caff43/db/save_data.py
-
curl -L -o save_data.py https://huggingface.co/spaces/Multimedika/Bot_Development/resolve/a0b918a749e8a598e36c4e05672bd60a10caff43/db/save_data.py
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 | |