Python/SQLite:将datetime.time变量插入时间类型的列时出错

mnowg1ta  于 2023-01-05  发布在  SQLite
关注(0)|答案(3)|浏览(232)

我在将datetime.time变量传递到SQLite数据库时遇到了麻烦,我这里有一些非常基本的代码来显示变量到底是什么。

import datetime as dt

time = dt.datetime.now().time() 
time = time.strftime('%H:%M') 
time = dt.datetime.strptime(time, '%H:%M').time()

print(time)
print(type(time))

time = dt.datetime.now().time()获取datetime.time类型中的当前时间。

  • 输出:*
17:34:48.286215
<class 'datetime.time'>

然后time = time.strftime('%H:%M')只检索小时和分钟,但类型为str

  • 输出:*
17:35
<class 'str'>

然后我用time = dt.datetime.strptime(time, '%H:%M').time()将其转换回datetime.time,得到输出:

17:32:00
<class 'datetime.time'>

Time类型的列接受文档(SQLite3 DateTime Documentation)中显示的HH:SS格式,因此我不确定为什么会出现此错误:

sqlite3.InterfaceError: Error binding parameter 11 - probably unsupported type.

在此INSERT语句中:

cursor.execute("INSERT INTO booked_tickets VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", (booking_ref, ticket_date, film, showing, ticket_type, num_tickets, cus_name, cus_phone, cus_email, ticket_price, booking_date, booking_time, ))

EDIT:根据请求,下面是一段代码片段,用于重新创建包含损坏列的表:

import datetime as dt
import sqlite3

connection = sqlite3.connect("your_database.db")
cursor = connection.cursor()

# Get the current time
time = dt.datetime.now().time()

# Format the time as a string using the '%H:%M' format
time_str = time.strftime('%H:%M')

# Parse the string back to a time object using the '%H:%M' format
time = dt.datetime.strptime(time_str, '%H:%M').time()

# Create the table
cursor.execute("CREATE TABLE test (example_time Time)")

# Insert the time into the example_time column
cursor.execute("INSERT INTO test VALUES (?)", (time, ))

connection.commit()

connection.close()
p1tboqfb

p1tboqfb1#

检查SQLite datatypes documentation
2.2.日期和时间数据类型
SQLite没有专门用于存储日期和/或时间的存储类。相反,SQLite的内置日期和时间函数能够将日期和时间存储为TEXT、真实的或INTEGER值:

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

公历。INTEGER表示Unix时间,是自1970-01-01 00:00:00 UTC以来的秒数。
应用程序可以选择以这些格式中的任何一种存储日期和时间,并使用内置的日期和时间函数在格式之间自由转换。
将日期存储为TEXT数据类型。

hpcdzsge

hpcdzsge2#

没有日期或时间data type in SQLite
您问题中的链接中的文档明确指出,在SQLite中,您可以通过3种方式存储日期时间:ISO-8601格式文本、整数unix历元和浮点julian日。
如果你选择第一种方式,那么你应该传递字符串:

booking_date = dt.datetime.now().date().strftime('%Y-%m-%d')
booking_time = dt.datetime.now().time().strftime('%H:%M:00') 
sql = "INSERT INTO booked_tickets VALUES (?,?,?,?,?,?,?,?,?,?)"
cursor.execute(sql, (booking_ref, ticket_date, film, showing, ticket_type, num_tickets, cus_name, cus_phone, cus_email, ticket_price, booking_date, booking_time))

但是,您也可以让SQLite获取当前日期和/或时间。
假设在booking_datebooking_time列中需要当前日期和时间,则可以将这些列定义为:

booking_date TEXT NOT NULL DEFAULT CURRENT_DATE, 
booking_time TEXT NOT NULL DEFAULT CURRENT_TIME

然后你不需要在INSERT语句中为它们传递任何东西:

sql = "INSERT INTO booked_tickets VALUES (?,?,?,?,?,?,?,?,?,?)"
cursor.execute(sql, (booking_ref, ticket_date, film, showing, ticket_type, num_tickets, cus_name, cus_phone, cus_email, ticket_price,))
3htmauhk

3htmauhk3#

您参考的文档主要讨论如何设置表示日期和时间的列值的格式。也就是说,它讨论如何处理数据库中已存在的日期和时间。
然而,我认为它确实给予了足够的信息来帮助你。它说:
日期和时间值可存储为

  • ISO-8601格式的子集中的文本,
  • 表示儒略日的数字,或
  • 表示自1970-01-01 00:00:00 UTC(UNIX时间戳)以来(或之前)的秒数的数字。

因此,您希望将日期和时间定义为完整的ISO-8601日期字符串或数字并提供。在定义表时,您可以通过将列类型分别定义为STRING、真实的或INTEGER来指明要使用哪种格式。
以下文档讨论了如何以下列格式之一存储日期和时间:https://www.sqlitetutorial.net/sqlite-date/

相关问题