N
N
Nikolai Neizvestny2020-05-21 18:43:00
Python
Nikolai Neizvestny, 2020-05-21 18:43:00

'NoneType' object is not subscriptable can't figure out why?

'NoneType' object is not subscriptable after execution

Here is the function from the bot

def del_cat(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]
            category = func.AddCategory(name)
            cat_dict[message.chat.id] = category

            conn = sqlite3.connect("base_ts.sqlite")
            cursor = conn.cursor()
            cursor.execute(f'SELECT * FROM "{name}"')
            row = cursor.fetchall()
            cursor.close()
            conn.close()

            text = ''
            num = 0

            for i in row:
                text = text + '№ ' + str(num) + '   |  ' + str(i[0]) + '\n'
                num += 1

            msg = bot.send_message(chat_id=message.chat.id,
                                   text='Выберите номер товара который хотите удалить\n\n'
                                        f'{text}')
            bot.register_next_step_handler(msg, del_cat_2)
        except Exception as e:
            print(e)
            bot.send_message(chat_id=message.chat.id,
                             text='Упсс, что-то пошло не по плану')

    def del_cat_2(message):
        try:
            category = cat_dict[message.chat.id]

            conn = sqlite3.connect("base_ts.sqlite")
            cursor = conn.cursor()
            cursor.execute(f"SELECT * FROM '{category.section}'")
            row = cursor.fetchall()

            name_category = row[int(message.text)][2]
            category.category = name_category

            markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
            markup.add('Yes', 'No')

            msg = bot.send_message(chat_id=message.chat.id,
                                   text='❕Удалить ⬇️\n'
                                        f'❕{category.category}\n\n'
                                        '❕из раздела ⬇️\n'
                                        f'❕{category.section}  ?',
                                   reply_markup=markup)
            bot.register_next_step_handler(msg, del_cat_3)
        except Exception as e:
            print(e)
            bot.send_message(chat_id=message.chat.id,
                             text='Упсс, что-то пошло не по плану')

    def del_cat_3(message):
        try:
            if message.text == 'Yes':
                category = cat_dict[message.chat.id]

                func.del_cat_to_section(category.category, category.section)
                bot.send_message(
                    chat_id=message.chat.id,
                    text=f'✅Товар: {category.category}\n'
                         f'✅Успешно удален из раздела',
                    reply_markup=menu.admin_menu
                )
            if message.text == 'No':
                bot.send_message(chat_id=message.chat.id,
                                 text='Вы вернулись в меню админа',
                                 reply_markup=menu.admin_menu)
        except Exception as e:
                print(e)
                bot.send_message(chat_id=message.chat.id,
                                 text='Упсс3, что-то пошло не по плану')

This is from importing the file as a function, I suspect that the problem is in the classes, but how will it be true
class AddCategory:
    def __init__(self, section):
        self.section = section
        self.cat = None
        self.category = None

class Category:
    def __init__(self, category):
        self.category = category

def del_cat_to_section(section, name_category):
    # Connection
    conn = sqlite3.connect("base_ts.sqlite")
    cursor = conn.cursor()

    # del
    category = cursor.execute(f'SELECT * FROM "{section}" WHERE list = "{name_category}"').fetchone()

    cursor.execute(f"DELETE FROM '{section}' WHERE list = '{name_category}'")
    conn.commit()

    cursor.execute(f"DROP TABLE '{category[2]}'")

    # Close connection
    cursor.close()
    conn.close()

I get to the 3rd divider and
'NoneType' object is not subscriptable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-05-21
@NHide

Stop doing that, learn the basics of relational databases. You don't need to create/delete tables for each category, you need to use links (relationships) between tables.
ZY well and standard, full traceback of an error is necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question