@NHide
Начинаю изучать python

Добавление под категорий,структура и организация запросов sqlite?

Помогите добавить под категорию пожалуйста второй день нон-стопом дерзаю интернет, все в голове напуталось.
sqlite категории
def add_section_to_catalog(name_section):
    conn = sqlite3.connect("base_ts.sqlite")
    cursor = conn.cursor()
    code = random.randint(11111, 99999)
    cursor.execute(f"INSERT INTO catalog VALUES ('{name_section}', '{code}')")
    conn.commit()
    conn.execute(f"CREATE TABLE '{code}' (list text, price text, code text)")
    cursor.close()
    conn.close()

Класс каталога,продукта и адд продукта
class Catalog:
    def __init__(self, name):
        self.name = name
class Product:
    def __init__(self, user_id):
        self.user_id = user_id
        self.product = None
        self.section = None
        self.price = None
        self.amount = None
        self.amount_MAX = None
        self.code = None
class AddProduct:
    def __init__(self, section):
        self.section = section
        self.product = None
        self.price = None
        self.info = None

sqlite продукта который добавляю в категорию
def add_product_to_section(name_product, price, name_section, info):
    conn = sqlite3.connect("base_ts.sqlite")
    cursor = conn.cursor()
    code = random.randint(11111, 99999)
    cursor.execute(f"INSERT INTO '{name_section}' VALUES ('{name_product}', '{price}', '{code}')")
    conn.commit()
    cursor.execute(f"INSERT INTO 'section' VALUES ('{name_product}', '{name_section}', '{code}', '{info}')")
    conn.commit()
    cursor.execute(f"CREATE TABLE '{code}' (list text, code text)")
    cursor.close()
    conn.close()

def create_product(message):
        try:
            conn = sqlite3.connect("base_ts.sqlite")
            cursor = conn.cursor()
            cursor.execute('SELECT * FROM catalog')
            row = cursor.fetchall()
            cursor.close()
            conn.close()
            name = row[int(message.text)][1]
            num_catalog = func.Product(name)
            product_dict[message.chat.id] = num_catalog
            addproduct = product_dict[message.chat.id]
            addproduct.section = name
            msg = bot.send_message(chat_id=message.chat.id,
                                   text=f'{name}\nВведите название товара')
            bot.register_next_step_handler(msg, create_product_2)
        except Exception as e:
            bot.send_message(chat_id=message.chat.id,
                             text='Упсс, что-то пошло не по плану')

    def create_product_2(message):
        try:
            product_name = message.text
            product = product_dict[message.chat.id]
            product.product = product_name
            msg = bot.send_message(chat_id=message.chat.id,
                                   text='Введите цены на товар')
            bot.register_next_step_handler(msg, create_product_3)
        except Exception as e:
            bot.send_message(chat_id=message.chat.id,
                             text='Упсс, что-то пошло не по плану')

    def create_product_3(message):
        try:
            price = message.text
            product = product_dict[message.chat.id]
            product.price = price
            msg = bot.send_message(chat_id=message.chat.id,
                                   text='Введите описание товара')
            bot.register_next_step_handler(msg, create_product_4)
        except Exception as e:
            bot.send_message(chat_id=message.chat.id,
                             text='Упсс, что-то пошло не по плану')

    def create_product_4(message):
        try:
            product = product_dict[message.chat.id]
            product.info = message.text
            markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
            markup.add('Yes', 'No')
            product_name = f'{product.product} | {product.price} руб'
            msg = bot.send_message(chat_id=message.chat.id,
                                   text=f'{product_name}\n\n'
                                        'Создать?',
                                   reply_markup=markup)
            bot.register_next_step_handler(msg, create_product_5)
        except Exception as e:
            bot.send_message(chat_id=message.chat.id,
                             text='Упсс, что-то пошло не по плану')

    def create_product_5(message):
        try:
            if message.text == 'Yes':
                product = product_dict[message.chat.id]
                product_name = f'{product.product} | {product.price} руб'

                func.add_product_to_section(product_name, product.price, product.section, product.info)
                bot.send_message(
                    chat_id=message.chat.id,
                    text=f'✅Товар: {product_name}\n'
                         f'✅Успешно добавлен в раздел',
                    reply_markup=menu.admin_menu
                )
        except Exception as e:
            print(e)
            bot.send_message(chat_id=message.chat.id,
                             text='Упсс, что-то пошло не по плану')

sql категории
def create_section(message):
        try:
            name = message.text
            catalog = func.Catalog(name)
            catalog_dict[message.chat.id] = catalog
            markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
            markup.add('Yes', 'No')
            msg = bot.send_message(chat_id=message.chat.id,
                                   text=name + '\n\n Создать?',
                                   reply_markup=markup)
            bot.register_next_step_handler(msg, create_section_2)
        except Exception as e:
            bot.send_message(chat_id=message.chat.id,
                             text='⚠️ Что-то пошло не по плану',
                             reply_markup=menu.main_menu)

    def create_section_2(message):
        try:
            if message.text == 'Yes':
                catalog = catalog_dict[message.chat.id]
                func.add_section_to_catalog(catalog.name)
                bot.send_message(
                    chat_id=message.chat.id,
                    text=f'✅Раздел: {catalog.name}\n'
                         f'✅Успешно добавлен в каталог',
                    reply_markup=menu.admin_menu
                )
        except Exception as e:
            print(e)

Хочу добавить подкатегории
что бы создать категорию в нее под категорию и в него уже продукт помогите пжл
  • Вопрос задан
  • 122 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы