Answer the question
In order to leave comments, you need to log in
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
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?
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?
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)# сюда придет уведомление о результатах рассылки
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);
cur.execute('''INSERT INTO users(user_name) VALUES(?)''', (user_name, ))
CREATE TABLE users (
user_id INTEGER UNIQUE, # если планируется самостоятельно заносить id
user_name TEXT);
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 questionAsk a Question
731 491 924 answers to any question