S
S
Studentka19962020-01-27 20:45:33
Python
Studentka1996, 2020-01-27 20:45:33

How to add User_Cart to the table by clicking on the button (product)??

Hello. I tried with the choice of quantity., in the end, at least 1 product how to add? A user-SELECTED item... I don't understand. I send an SQL query that displays

the products that are in stock. Product_code ''') re_Pizs = cur.fetchall() #record with code 2, 4, 7, 8 Display results in inline buttons. What condition to write in order to select a specific one! goods add a record to the table. I explicitly specify which element to display when clicked, but this is not correct (( File Main:

#Соединяемся с БД 
    con = funcs_DB.sqlite3.connect(funcs_DB.name_DB) 
    cur = con.cursor()
            #Имя и фамилия пользователя
    user_name =  message.chat.first_name #Получим имя юзера
    last_name =  message.chat.last_name 


    insert_query_Korzina = '''INSERT OR IGNORE INTO Корзина_юзера(Товар,
        Цена_руб, Количество, Юзер) VALUES (?, ?, ?,  ?)''' 
    lt_one = (funcs_DB.re_Naps[0])
    lt_two = (1, f'{user_name} {last_name}') 
    list_Korzina= [lt_one + lt_two]

#Добавление товаров в корзину_Юзера
    if text == 'НапиткиAdd':
        count = cur.executemany(insert_query_Korzina, list_Korzina)
        con.commit()
        bot.send_message(message.chat.id, f'Товар: {list_Korzina[0]} добавлен в корзину.') 
    print("Запись успешно вставлена ​​в таблицу: Корзина_юзера\n", "Кол-во записей:" ,  cur.rowcount)


I forgot. Implementation of the inline menu:
for row in funcs_DB.re_Naps:
            #Создаём инлайн меню_1 
    keyboard_inline_menu_Naps.add( 
        telebot.types.InlineKeyboardButton(f'{row[0]} Цена: {row[1]}', callback_data='НапиткиAdd'))


Do not judge strictly, I understand that inline buttons are superfluous, but at least it’s more or less clear what product I’m adding. availability is also a plus. Although, there are different ways to solve, I understand :)
I really, really ask for help ....

5e2f226384fe6536804271.png
5e2f229c1e97b357495831.png

The problem is solved. Ivan, thank you so much!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Yakushenko, 2020-01-27
@Studentka1996

Chet is difficult to understand all this, looking only at some strange snatches. Anyway. As I understand it re_Naps, you have a list of goods for output, if so, then here lt_one = (funcs_DB.re_Naps[0])you yourself indicate that you need to write down exactly the first element from the list of these goods.
Further: you really cannot pass to the callbacktext of the button, only what is in callback_data, but you can do something like this:
Create buttons:

keyboard_inline_menu_Naps.add( 
    telebot.types.InlineKeyboardButton(
        f'{row[0]} Цена: {row[1]}', callback_data=f'НапиткиAdd_{row[0]}_{row[1]}'
    )
)

Further, as I understand it, textyou save yours callbackfrom the button, if so, then here:
if 'НапиткиAdd' in text:
    callback_data = text.split('НапиткиAdd_')[-1].split('_')
    list_Korzina = [*callback_data, 1, f'{user_name} {last_name}']
    count = cur.execute(insert_query_Korzina, list_Korzina)
    con.commit()
    bot.send_message(message.chat.id, f'Товар: {list_Korzina[0]} добавлен в корзину.')

Again, if I understand the code correctly and what is needed, then this option should work.

E
Evloshevsky Nikolay, 2020-01-27
@n1k_crimea

not to the point of the question, but why not start using sqlalchemy?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question