T
T
Turkmen Time2020-10-24 20:08:01
Python
Turkmen Time, 2020-10-24 20:08:01

How to solve the problem in telebot/sqlite3?

How can I make sure that all the people who started the bot are entered into the database?

import sqlite3
import telebot


bot = telebot.TeleBot('Токен')
db = sqlite3.connect('TG.db', check_same_thread=False)
sql = db.cursor()

sql.execute("""CREATE VIRTUAL TABLE IF NOT EXISTS Started USING fts4(
    id TEXT,
    username TEXT
)""")

@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id, 'Привет')
    
    username = '{0}{1}'.format('@', message.from_user.username)
    uid = message.chat.id
    
    sql.execute(f"SELECT id FROM Started WHERE id = '{uid}'")
    
    if sql.fetchone() is None:
        sql.execute(f"INSERT INTO Started VALUES (?,?)", (username, uid))
    db.commit()

bot.polling()

I made such a code for some reason brings a person into the database, even if he is already there

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-10-24
@MrSel

What about search? Here chewed here . And at least you need a primary key in the table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question