K
K
kreeplambor2021-12-26 08:03:30
Python
kreeplambor, 2021-12-26 08:03:30

How to edit entries in sqlite via python?

I'm trying to add the ability to add a phone number in the bot, I can't figure out how to edit the phone_number field .

db = sqlite3.connect('Товар.db', check_same_thread=False)
sql = db.cursor()
@bot.message_handler(commands = ['number'])
def Запрос_номер_телефона(message):
  zapros = sql.execute(f"SELECT Номер_телефона FROM Пользователи WHERE user_id = '{message.from_user.id}'") 
  number = sql.fetchone()
  print('number', number)
  if number is None:
    bot.send_message(message.chat.id, 'Хотите оставить номер телефона?', reply_markup = markup_reg1)
    if message.text == 'Оставить номер':
      bot.send_message(message.chat.id, 'Отправить номер из телеграм или написать?', reply_markup = markup_reg2)
      if message.text == 'Номер из телеграм':
        db.execute("UPDATE Пользователи Номер_телефона = ?", 
          (message.contact.phone_number))
      elif message.text == 'Ввести':
        bot.send_message(message.chat.id, 'Вводите')
        db.execute("UPDATE Пользователи Номер_телефона = ?",
          (message.text))
      db.commit()
      print(f'Номер пользователя {message.from_user.username} зарегистрировын')
    elif message.text == 'Нет': 
      bot.send_message(message_handler.chat.id, 'Хорошо')
  else: 
    bot.send_message(message.chat.id, 'Номер телефона уже зарегистрировын.')

bot.polling(none_stop = True, interval = 0)

bot.polling(none_stop = True, interval = 0)

Even if the "Phone_number" field is empty, it still displays a message that the number is already registered.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-12-26
@phaggi

Do I understand correctly, you have a flat table Users, where each user has a row, each user has attributes (columns) user_name, user_id, Name, Phone_number
Then in the row

db.execute("INSERT INTO Пользователи (Номер_телефона) VALUES (?)",
          (message.text))
you are trying to add a row, but you need to update an already created row, similar to the update from the previous branch.
In fact, you need to leave one line with update after all if / elif, where you find out for the phone number, and pass the received value of the phone number to this line (it does not matter whether it was received from telegrams or from the user).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question