Answer the question
In order to leave comments, you need to log in
I am making a bot for recording homework in Telegram. I am using the sqlite3 library. Where is the error in the code and how to fix it?
Hello!
I am writing a bot for recording dz. I am using the sqlite3 library. At the same time, I don’t really understand the syntax of this library.
The bot should write the text sent by the user to the column defined by the code. In another part of the code
, it should access the last filled cell of the required column and send its text to the user in
one message like:
import telebot
import constants
import sqlite3
from telebot import apihelper
proxy = constants.ip+":"+constants.port
apihelper.proxy = {'https': proxy}
bot = telebot.TeleBot(constants.token1)
conn = sqlite3.connect("mydatabase.db")
cursorObj = conn.cursor()
cursorObj.execute("CREATE TABLE IF NOT EXISTS dz (Algebra text, Geom text, Rus text, Lit text, Him text, Fiz text, Nem text, Istor text, Obshh text, Geogt text, Inf text, AnglSA text, AnglTA text, Fizra text, OBZH text)")
@bot.message_handler(commands=['admin'])
def hendle_admin(message):
user_markup = telebot.types.ReplyKeyboardMarkup(True, True)
user_markup.row('Отмена')
msg = bot.send_message(message.chat.id, "Введите пароль", reply_markup = user_markup)
bot.register_next_step_handler(msg, handle_admin_password)
def handle_admin_password(message):
if message.text == constants.password:
bot.send_message(message.chat.id, "Пароль верен")
user_markup = telebot.types.ReplyKeyboardMarkup(True, True)
user_markup.row('Алгебра', 'Геометрия', 'Русский', 'Литература')
user_markup.row('Химия', 'Физика', 'Немецкий', 'Биология')
user_markup.row('История', 'Обществознание', 'География', 'Информатика')
user_markup.row('АнглийскийСА', 'АнглийскийТА', 'Физкультура', 'Обж')
user_markup.row('/start')
msg = bot.send_message(message.chat.id, "Выберите предмет", reply_markup=user_markup)
bot.register_next_step_handler(msg, handle_admin_dz)
elif message.text == 'Отмена':
user_markup = telebot.types.ReplyKeyboardMarkup(True, True)
user_markup.row('/admin')
user_markup.row('/start')
bot.send_message(message.chat.id, "Вы уверенны?", reply_markup = user_markup)
def handle_admin_dz(message):
if message.text == "Алгебра":
cursorObj = conn.cursor()
global algebra
algebra = bot.send_message(message.chat.id, "Запишите дз")
cursorObj.execute('INSERT INTO dz VALUES (?, )', (algebra, ))
conn.commit()
elif message.text == "Геометрия":
Geom = bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "Русский":
Rus = bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "Литература":
Lit = bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "Химия":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "Физика":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "Немецкий":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "История":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "Обществознание":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "География":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "Информатика":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "АнглийскийСА":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "АнглийскийТА":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "Физкультура":
bot.send_message(message.chat.id, "Запишите дз")
elif message.text == "Обж":
bot.send_message(message.chat.id, "Запишите дз")
@bot.message_handler(commands=['help', 'start'])
def hendle_start(message):
user_markup = telebot.types.ReplyKeyboardMarkup(True, True)
user_markup.row('Понедельник', 'Вторник', 'Среда')
user_markup.row('Четверг', 'Пятница', 'Суббота')
user_markup.row('/admin')
bot.send_message(message.chat.id, "Выберите день недели", reply_markup = user_markup) #включен
@bot.message_handler(content_types='text')
def hendle_text(message):
if message.text == "Понедельник":
den = 'ПОНЕДЕЛЬНИК\n#Алгебра: ' + handle_admin_dz.algebra
#Физика:
#Обществознание:
#История:
#Информатика:'
bot.send_message(message.chat.id, den)
elif message.text == "Вторник":
den = 'ВТОРНИК' \
bot.send_message(message.chat.id, den)
elif message.text == "Среда":
den = 'СРЕДА'
bot.send_message(message.chat.id, den)
elif message.text == "Четверг":
den = 'ЧЕТВЕРГ'
bot.send_message(message.chat.id, den)
elif message.text == "Пятница":
den = 'ПЯТНИЦА'
bot.send_message(message.chat.id, den)
elif message.text == "Суббота":
den = 'СУББОТА'
bot.send_message(message.chat.id, den)
bot.polling(none_stop=True, interval=0)
Answer the question
In order to leave comments, you need to log in
Well, I understand that telebot creates threads when called, so I think that everything will work if you create a new connection to the database in the right thread (In the handle_admin_dz function). And this block with elif can be simply removed, since when choosing any item, the same DZ is sent and replaced simply with If in DZlist:
DZlist = ['Геометрия', 'Русский' ......]
if message.text in DZlist:
bot.send_message(message.chat.id, "Запишите дз")
else:
pass
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question