You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将datetime.time变量插入SQLite Time类型列时出现接口错误

问题:无法将datetime.time类型变量存入SQLite数据库

我尝试把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))

各步骤输出

  1. 执行time = dt.datetime.now().time()获取当前时间,输出:
17:34:48.286215
<class 'datetime.time'>
  1. 执行time = time.strftime('%H:%M')提取小时和分钟,此时变量类型转为字符串,输出:
17:35
<class 'str'>
  1. 执行time = dt.datetime.strptime(time, '%H:%M').time()转回datetime.time类型,输出:
17:32:00
<class 'datetime.time'>

错误信息

根据SQLite文档,Time类型列支持HH:MM格式,但执行INSERT语句时出现以下错误:

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, ))

复现代码片段

import datetime as dt
import sqlite3

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

# 获取当前时间
time = dt.datetime.now().time()

# 格式化为HH:MM字符串
time_str = time.strftime('%H:%M')

# 转换回datetime.time类型
time = dt.datetime.strptime(time_str, '%H:%M').time()

# 创建测试表
cursor.execute("CREATE TABLE test (example_time Time)")

# 插入时间变量
cursor.execute("INSERT INTO test VALUES (?)", (time, ))

connection.commit()
connection.close()

解决方案

SQLite的Python标准库sqlite3默认不直接支持datetime.time类型的绑定,即便文档说明Time列支持HH:MM格式,也需要手动处理类型转换:

方法1:直接存入格式化后的字符串

跳过转回datetime.time的步骤,直接使用strftime('%H:%M')生成的字符串插入数据库:

# 替换变量处理逻辑
time_str = dt.datetime.now().time().strftime('%H:%M')
# 插入时直接传time_str
cursor.execute("INSERT INTO test VALUES (?)", (time_str, ))

方法2:注册自定义类型转换器

通过sqlite3.register_adapter和sqlite3.register_converter让sqlite3支持datetime.time类型:

import datetime as dt
import sqlite3

# 注册适配器:把datetime.time转为字符串
def adapt_time(time_obj):
    return time_obj.strftime('%H:%M:%S')

# 注册转换器:把数据库中的字符串转回datetime.time
def convert_time(time_str):
    return dt.datetime.strptime(time_str.decode(), '%H:%M:%S').time()

sqlite3.register_adapter(dt.time, adapt_time)
sqlite3.register_converter("TIME", convert_time)

# 连接数据库时需指定detect_types参数
connection = sqlite3.connect("your_database.db", detect_types=sqlite3.PARSE_DECLTYPES)
cursor = connection.cursor()

# 后续操作不变
time = dt.datetime.now().time().strftime('%H:%M')
time = dt.datetime.strptime(time, '%H:%M').time()

cursor.execute("CREATE TABLE test (example_time TIME)")
cursor.execute("INSERT INTO test VALUES (?)", (time, ))

connection.commit()
connection.close()

方法3:用datetime.datetime代替datetime.time

如果业务允许,可以把时间包装成datetime.datetime类型(比如绑定一个任意日期),SQLite的sqlite3库默认支持datetime.datetime类型的存储和读取:

# 把time转为datetime(用当前日期)
dt_time = dt.datetime.combine(dt.date.today(), time)
# 插入时传dt_time
cursor.execute("INSERT INTO test VALUES (?)", (dt_time, ))

内容的提问来源于stack exchange,提问作者user17221554

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 20:25:11