@h0tgaysex

Почему не работает база данных sqlite3 для телеграм бота?

после добавления бага он не отправляет сообщение что его сохранил, в таблице его не появляется, и собственно найти и изменить он его тоже исходя из этого не может, какая может быть проблема с бд?

import sqlite3
import telebot

bot = telebot.TeleBot("")
conn = sqlite3.connect('bugs.db')
cursor = conn.cursor()
cursor.execute('''
    CREATE TABLE IF NOT EXISTS bugs (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        bug_name TEXT UNIQUE,
        bug_info TEXT,
        developer TEXT
    )
''')
conn.commit()



@bot.message_handler(commands=['add_bug'])
def add_bug(message):
    msg = bot.send_message(message.chat.id, "Enter the bug name:")
    bot.register_next_step_handler(msg, process_bug_name)

def process_bug_name(message):
    global bug_name_var
    bug_name_var = message.text
    msg = bot.reply_to(message, "Enter the bug information:")
    bot.register_next_step_handler(msg, process_bug_info)

def process_bug_info(message):
    global bug_info_var
    bug_info_var = message.text
    msg = bot.reply_to(message, "Enter the developer's name:")
    bot.register_next_step_handler(msg, process_developer)

def process_developer(message):
    developer = message.text

    cursor.executemany('''
        INSERT INTO bugs (bug_name, bug_info, developer) VALUES (?, ?, ?)
    ''', (bug_name_var, bug_info_var, developer))
    conn.commit()

    bot.send_message(message.chat.id, "Bug information successfully saved.")

@bot.message_handler(commands=['view_bug'])
def view_bug_info(message):
    msg = bot.reply_to(message, "Enter the bug name to view:")
    bot.register_next_step_handler(msg, process_view_bug_info)

def process_view_bug_info(message):
    bug_name = message.text.strip()

    if bug_name:
        cursor.executemany("SELECT * FROM bugs WHERE bug_name=?", (bug_name,))
        bug_data = cursor.fetchone()

        if bug_data:
            bot.send_message(message.chat.id, f"Bug information for '{bug_data[1]}':\n{bug_data[2]}\nDeveloper: {bug_data[3]}")
        else:
            bot.send_message(message.chat.id, "Bug with that name was not found.")
    else:
        bot.send_message(message.chat.id, "Please enter a valid bug name.")


@bot.message_handler(commands=['update_bug'])
def update_bug_info(message):
    msg = bot.send_message(message.chat.id, "Enter the bug name to update its information:")
    bot.register_next_step_handler(msg, process_update_bug_info)

def process_update_bug_info(message):
    global bug_name_var
    bug_name = message.text.strip()
    cursor.executemany("SELECT * FROM bugs WHERE bug_name=?", (bug_name,))
    bug_data = cursor.fetchone()

    if bug_data:
        global bug_name_var
        bug_name_var = bug_name

        bot.send_message(message.chat.id, f"Enter the new information for the bug '{bug_name}' and the new developer's name in the format: 'New Info New Developer'.")
        bot.register_next_step_handler(message, update_bug_info_final)
    else:
        bot.send_message(message.chat.id, "Bug with that name was not found.")

def update_bug_info_final(message):
    global bug_name_var
    update_data = message.text.split()
    
    if len(update_data) != 2:
        bot.send_message(message.chat.id, "Please enter the new bug information and developer name in the correct format.")
        return
    
    new_bug_info, new_developer = update_data

    cursor.executemany("UPDATE bugs SET bug_info=?, developer=? WHERE bug_name=?", (new_bug_info, new_developer, bug_name_var))
    conn.commit()
    bot.send_message(message.chat.id, f"Information for bug '{bug_name_var}' successfully updated.")


@bot.message_handler(commands=['delete_bug'])
def delete_bug_info(message):
    msg = bot.send_message(message.chat.id, "Enter the bug name to delete from the database:")
    bot.register_next_step_handler(msg, process_delete_bug_info)

def process_delete_bug_info(message):
    bug_name = message.text.strip()

    cursor.executemany("DELETE FROM bugs WHERE bug_name=?", (bug_name,))
    if cursor.rowcount > 0:
        conn.commit()
        bot.send_message(message.chat.id, f"Information for bug '{bug_name}' successfully deleted from the database.")
    else:
        bot.send_message(message.chat.id, f"No bug found with the name '{bug_name}'. No data deleted.")

bot.polling()
  • Вопрос задан
  • 112 просмотров
Пригласить эксперта
Ответы на вопрос 1
Vindicar
@Vindicar
RTFM!
cursor.executemany('''
        INSERT INTO bugs (bug_name, bug_info, developer) VALUES (?, ?, ?)
    ''', (bug_name_var, bug_info_var, developer))


Почему тут executemany()? Ты вызываешь его, как будто это простой execute().
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы