import telebot, datetime, sqlite3
from telebot import types
token = ''
bot = telebot.TeleBot(token)
start_markup = types.InlineKeyboardMarkup(row_width=2)
start_markup.add(types.InlineKeyboardButton(text='Время уведомлений', callback_data='message_time'), types.InlineKeyboardButton(text='Время занятия', callback_data='learn_time'), types.InlineKeyboardButton(text='Статистика', callback_data='stats'), types.InlineKeyboardButton(text='Начать', callback_data='start_act'))
time_markup = types.ReplyKeyboardMarkup(row_width=4)
time_markup.add(types.KeyboardButton(text='8'), types.KeyboardButton(text='12'), types.KeyboardButton(text='16'), types.KeyboardButton(text='20'))
message_markup = types.InlineKeyboardMarkup(row_width=1)
message_markup.add(types.InlineKeyboardButton(text='Начать', callback_data='start_act'))
end_message_markup = types.InlineKeyboardMarkup(row_width=1)
end_message_markup.add(types.InlineKeyboardButton(text='Закончить', callback_data='end_act'))
# new_time = 0
start_time = 0
end_time = 0
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, 'Выбери пункт ниже', reply_markup=start_markup)
@bot.callback_query_handler(func = lambda call: True)
def main_work(call):
if call.data == 'message_time':
# msg = bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text=f'Текущее время напоминания - {new_time}:00\nНапишите время напомимания (час)')
# bot.register_next_step_handler(msg, change_time)
pass
if call.data == 'learn_time':
pass
if call.data == 'stats':
pass
if call.data == 'start_act':
global start_time
s_t = datetime.datetime.now()
start_time = s_t.hour * 60 + s_t.minute
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text='Отсчёт пошёл', reply_markup=end_message_markup)
today = datetime.datetime.now().date()
conn = sqlite3.connect(r'programm-bot/main.db')
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM days WHERE day = ?", ('{today}',))
if cursor.fetchone():
pass
else:
cursor.execute(f"INSERT INTO days VALUES ('{today}', 0)")
if call.data == 'end_act':
global end_time
today = datetime.datetime.now().date()
e_t = datetime.datetime.now()
end_time = e_t.hour * 60 + e_t.minute
total = end_time - start_time
conn = sqlite3.connect(r'programm-bot/main.db')
cursor = conn.cursor()
cursor.execute(f"SELECT learn_time FROM days WHERE day = {today}") # вытаскивание кол-во минут сегодня
today_time = cursor.fetchone()[0]
today_time += total
cursor.execute(f"UPDATE days SET learn_time = {today_time} WHERE day = {today}")
conn.commit()
conn.close()
bot.polling(non_stop=True)
Выдаёт ошибку: sqlite3.IntegrityError: UNIQUE constraint failed: days.day
Как я понимаю. всё крашиться в момент добавления строки day