S
S
shuzzixd2021-08-08 17:56:13
Python
shuzzixd, 2021-08-08 17:56:13

I'm doing a mailing list in a bot. I'm getting this error, how can I fix it?

The code:

if message.text == "Сделать рассылку" and message.chat.id == ADMIN_ID:
        bot.send_message(message.chat.id, "Введите текст рассылки", reply_markup=adminmenu_send)
        bot.register_next_step_handler(message, mailing)

def mailing(message):
    if message.text == "Отменить" and message.chat.id == ADMIN_ID:
        bot.send_message(message.chat.id, "Отменено", reply_markup=adminmenu)
    elif message.text and message.chat.id == ADMIN_ID:
        text = message.text
        db = sqlite3.connect('users.db')
        sql = db.cursor()
        sql.execute("SELECT id FROM users")
        id = sql.fetchall()
        for id in id:
            for id in id:
               try:
                bot.send_message(id, f"{text}")
                time.sleep(1)
               except:
                   sql.execute(f"DELETE FROM users WHERE id = {id}")
        bot.send_message(ADMIN_ID, "Рассылка завершена", reply_markup=adminmenu)
        db.commit()

I am getting an error:
File "C:\Users\User\Desktop\bots\bot.py", line 780, in mailing
    sql.execute(f"DELETE FROM users WHERE id = {id}")
sqlite3.OperationalError: incomplete input

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-08
@shuzzixd

1. for id in id:
for id in id:
What kind of nonsense is this? What is id in the end?
2. f"DELETE FROM users WHERE id = {id}"
For the formation of queries through string formatting, you need to beat your hands with a ruler. Read how to use prepared statements in python.
My assumption: due to undercycles, an id equal to an empty string is obtained, and since you stick it into the query somehow, as a result, a query like "DELETE FROM users WHERE id = " goes to the database, which she rightly swears at.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question