-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
32 lines (26 loc) · 1.3 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import aiosqlite
from aiogram.types import Message
from pizza_data import images, names, descriptions, prices
from keyboards import inline
async def sql_start_db():
async with aiosqlite.connect('pizza_pub.db') as db:
if db:
print("DB was started(created)")
await db.execute(
'CREATE TABLE IF NOT EXISTS menu(img TEXT, name TEXT PRIMARY KEY, description TEXT, price TEXT);'
)
await db.commit()
async with db.execute('SELECT * FROM menu;') as cursor:
if len(await cursor.fetchall()) == 0:
for i in range(5):
await cursor.execute(
f'INSERT INTO menu VALUES ("{images[i]}", "{names[i]}", "{descriptions[i]}", "{prices[i]}")')
await db.commit()
print(f"Data_{i + 1} in db")
async def sql_read(message: Message):
async with aiosqlite.connect('pizza_pub.db') as db:
async with db.execute('SELECT * FROM menu;') as cursor:
async for ret in cursor:
await message.answer_photo(ret[0],
f'<b><u>{ret[1]}</u></b>\n\nОписание: {ret[2]}\nЦена: <em>{ret[-1]}</em> руб',
parse_mode='HTML', reply_markup=inline.inline_btns)