@Nikita235

Как добавить в таблицу sqlite3 дату?

Пишу бота на AIOgram и мне потребовалось записать дату в базу данный SQLite3.

Для выбора даты использую библиотеку telegram_bot_calendar.

Вот фрагмент кода:
@dp.callback_query_handler(DetailedTelegramCalendar.func(), state=Admin.date)
async def inline_kb_answer_callback_handler(query, state=FSMContext):
    result, key, step = DetailedTelegramCalendar(locale="ru").process(query.data)

    if not result and key:
        await bot.edit_message_text(f"Выберите дату",
                                    query.message.chat.id,
                                    query.message.message_id,
                                    reply_markup=key)
    elif result:
        await bot.edit_message_text(f"Задача назначена на {result}",
                                    query.message.chat.id,
                                    query.message.message_id)

        async with state.proxy() as data:
            data["date"] = query.message.text
        await sqlite_db.sql_add_command(state)
        await state.finish()


Вот сам код базы:
import sqlite3 as sq

def sql_start():
    global base, cur
    base = sq.connect("notific.db")
    cur = base.cursor()
    if base:
        print("Data base connected OK")
    base.execute("CREATE TABLE IF NOT EXISTS list(name_task TEXT, date TEXT)")
    base.commit()

async def sql_add_command(state):
    async with state.proxy() as data:
        cur.execute("INSERT INTO list VALUES (?, ?)", tuple(data.values()))
        base.commit()


А вот результат:
63824c50dd34e899157967.png

Записывается всё, кроме даты. В чем может быть дело?
  • Вопрос задан
  • 277 просмотров
Пригласить эксперта
Ответы на вопрос 1
@rPman
data["date"] = query.message.textты пишешь как текст как он приходит от пользователя
и да у sqlite нет типа date
2.2. Date and Time Datatype

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

Applications can choose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

судя по скрину там где ты это смотришь формат даты ожидается иной
Ответ написан
Ваш ответ на вопрос

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

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