I
I
ibragim_20082021-07-14 19:51:23
Python
ibragim_2008, 2021-07-14 19:51:23

Why doesn't sql query work in conditions?

I execute an INSERT query to SQlite in a condition, but the query is not executed, although the condition is met. when i execute the query outside the function and conditions everything works.
The code:

import sqlite3
import telebot

bot = telebot.TeleBot('1827293334:AAGHrPyNde1etaEjua3dcfGfoYUK4hVh20g')

connection = sqlite3.connect('telegram.db', check_same_thread=False)
cursor = connection.cursor()

@bot.message_handler(commands=['start'])
def start_command(message):
    db1 = cursor.execute(f"SELECT * FROM telegram_table WHERE id = {message.from_user.id}")

    if db1.fetchall() == []:
        if " " in message.text:
            referrer_id_str = message.text.split()[1]
            referrer_id = int(referrer_id_str)
            cursor.execute(f'INSERT INTO telegram_table VALUES(NULL,{message.from_user.id}, {referrer_id},"{message.from_user.username}")')
        else:
            cursor.execute(f'INSERT INTO telegram_table VALUES(NULL,{message.from_user.id}, {0},"{message.from_user.username}")')
    else:
        return
    user_markup = telebot.types.ReplyKeyboardMarkup(True, False)
    user_markup.row('Получить ссылку', 'Мои рефералы')
    bot.send_message(message.chat.id, f"Здравствуйте {message.from_user.first_name}, в данном боте вы можете получить реферальную ссылку чтобы делиться ею с другими людьми", reply_markup=user_markup)

@bot.message_handler(content_types=['text'])
def get_text_messages(message):
    if message.text == "Получить ссылку":
        bot.send_message(message.from_user.id, f"Ваша реферальная ссылка: https://t.me/TestPavel_bot?start={message.from_user.id}")
    elif message.text == "Мои рефералы":
        bot.send_message(message.from_user.id, "Напиши привет")
bot.polling(none_stop=True)
connection.commit()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-07-14
@ibragim_2008

After changing the data, you need to do
connection.commit()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question