【发布时间】:2017-03-24 23:58:05
【问题描述】:
我正在使用 SQLAlchemy,但遇到了 SQLite 错误的问题:
SQLite Date type only accepts Python date objects as input.
[SQL: 'SELECT anon_1.patient_sid AS sid FROM
(SELECT clinical_data.patient_sid AS patient_sid FROM clinical_data
WHERE clinical_data.event_date >= ?) AS anon_1']
我完全理解错误的含义,但我不明白为什么会发生这种情况。
我在上面clinical_data.event_date >= ?的查询中传递的用于进行日期比较的参数设置为:
valdate = datetime.strptime('1776-01-01 00:00:00', "%Y-%m-%d %H:%M:%S").date()
而且,我已经验证valdate 的数据类型是<type 'datetime.date'>
用于构造查询的类是:
class ClinicalData(db.Model):
__tablename__ = 'clinical_data'
id = Column(Integer, primary_key=True, autoincrement=True)
patient_id = Column(Integer)
patient_sid = Column(Integer)
string_value = Column(String(255))
double_value = Column(Float)
data_type_id = Column(Integer)
event_date = Column(Date)
ontology_id = Column(Integer)
attribute_id = Column(Integer)
project_id = Column(Integer)
replaced_by_id = Column(Integer)
date_record_added = Column(DateTime)
parent = Column(Integer)
num_children = Column(Integer)
lft = Column(Integer)
rgt = Column(Integer)
SQLite 的 SQLAlchemy 文档指出(请参阅 SQLAlchemy SQLite documentation)“在使用 SQlite 时,SQLAlchemy 自己的 DateTime 和相关类型提供日期格式和解析功能......”
我不是在问如何将我的字符串对象转换为 python 日期时间对象,我也不是在问错误的含义。当一切似乎都已充分定义时,我不确定为什么我一直收到此错误。
编辑
请注意,当我在我的模型中的 event_date 属性上使用 DateTime 时,我收到以下错误 SQLite DateTime type only accepts Python datetime and date objects as input. 为此,我在没有 date() 方法的情况下定义了 valdate = datetime.strptime('1776-01-01 00:00:00', "%Y-%m-%d %H:%M:%S")。正如预期的那样,type(valdate) 在这种情况下产生<type 'datetime.datetime'>。
我已经尝试了创建变量valdate 和我班级的event_date 属性的所有组合。
【问题讨论】:
标签: python sqlite datetime sqlalchemy