N
N
Nazar Ivakhiv2021-03-27 23:47:20
Bots
Nazar Ivakhiv, 2021-03-27 23:47:20

How to make the bot's people id not repeat in sqlite?

How to make the bot's people id not repeat in sqlite?

And then how to make a newsletter so that you can attach an inline button, a picture and a photo to it in one message?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Karbivnichy, 2021-03-28
@hottabxp

How to make the bot's people id not repeat in sqlite?
Before adding an id to the database, check for the presence of this id in the database.
And then how to make a newsletter so that you can attach an inline button, a picture and a photo to it in one message?

Read the docs. There are examples, too.

M
me, 2021-03-28
@dabudi

You can try like this

userid = message.chat.id
with sq.connect('base.db') as con:
    cur = con.cursor() 
    cur.execute('''SELECT userid FROM users''')
    ALLuser = cur.fetchall()

if userid in ALLuser:
    print('Такой ID уже есть')
else:
    with sq.connect('base.db') as con:
    cur = con.cursor() 
    cur.execute('''INSERT INTO users(userid) VALUES(?)''', (userid))

And then how to make a newsletter so that you can attach an inline button, a picture and a photo to it in one message?

Distribution can be done like this
with sq.connect('base.db') as con:
    cur = con.cursor()
    cur.execute('''SELECT userid FROM user''')
    AllUser = cur.fetchall()
    
count = 0
errors = 0

start_time = time.time()
for s in range(len(AllUser)):
    user_id = AllUser[count][0]
    try:
        bot.send_message(user_id, text='Текст для рассылки')
        count += 1
    except:
        count += 1
        errors += 1
        
allusers = int(len(dataid))
goodresult = allusers - errors
goodresult = str(goodresult)

errors = str(errors)
times = "Время выполнения: %s сек." % round((time.time() - start_time))
timesstr = times
sms = 'Рассылка завершена!'+'\n'+ 'Успешных результатов: '+goodresult+'\n'+'Ошибок: '+errors+'\n'+timesstr
bot.send_message(твой_айди, sms)# сюда придет уведомление о результатах рассылки

O
o5a, 2021-03-28
@o5a

For the SQL database, it would be more correct to declare the uniqueness of the field (UNIQUE / PRIMARY KEY), and then when inserting, you can not do independent checks, duplicates will not be entered anyway.
When creating a table:

CREATE TABLE users (
user_id INTEGER PRIMARY KEY # чтобы заполнялся автоматически, тогда вообще не нужно указывать id, он всегда будет проставляться автоматом новый
user_name TEXT);

Then when inserting, you don’t need to use user_id at all, it will be inserted automatically
cur.execute('''INSERT INTO users(user_name) VALUES(?)''', (user_name, ))

or
CREATE TABLE users (
user_id INTEGER UNIQUE, # если планируется самостоятельно заносить id
user_name TEXT);

Then you need to put down the user_id yourself, but you don’t have to do a duplicate check either if you specify OR IGNORE
cur.execute('''INSERT OR IGNORE INTO users(user_id, user_name) VALUES(?, ?)''', (user_id, user_name))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question