I am writing a bot on aiogram, I took PostreSQL as a DBMS. The database contains the product ID, photo, name, and description with price. I tried uploading the url of the photo to the database, but when output, it is necessary that the url is not output, but the image itself is output. Or is there another way to upload an image without a url?
Important: images must be stored in a database
import psycopg2
import asyncpg
import asyncio
import aiogram
from aiogram import types
class BD1:
def ss():
try:
conn = psycopg2.connect(host="localhost",
port=5432,
database="postgres",
user="postgres",
password="135")
cur = conn.cursor()
print("Database opened successfully")
# cur.execute("SELECT photo FROM proba")
# ph = cur.fetchone()[0]
# photo = types.InputFile(ph)
# await bot.send_photo(message.chat.id, photo, caption='Check out this photo!')
cur.execute("""SELECT * FROM perfumes""")
query_results = cur.fetchall()
# cur.execute("SELECT photo FROM proba ;")
# photo = cur.fetchone()[0]
#
# # Сохранение изображения в файл
# with open("image.jpg", "wb") as file:
# file.write(photo)
text = 'nn'.join(['nn'.join(map(str, x)) for x in query_results])
return (str(text))
except psycopg2.Error as e:
print("Error connecting to database:", e)
finally:
cur.close()
conn.close()
# cur.execute("""SELECT * FROM perfumes""")
# query_results = cur.fetchall()
# text = 'nn'.join([', '.join(map(str, x)) for x in query_results])
# return (str(text))
handlers.py:
@router.callback_query()
async def callback_keyboard(callback: CallbackQuery):
if callback.data == 'teas':
await callback.answer()
await callback.message.answer(BD1.ss())
WXXX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.