SQLAlchemy的Python数据时间对象问题 [英] Python datatime object issue with SQLAlchemy
问题描述
我正在使用SQLAlchemy,并且遇到SQLite错误的问题:
I am using SQLAlchemy and am having an issue wrt to an SQLite error:
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']
我完全理解错误的含义,但是我不明白为什么我会遇到这种错误。
I understand perfectly the meaning of the error, but I do not understand why it is happening in my case.
我在 clinical_data.event_date> =?
上面的查询中传递的用于进行日期比较的参数设置为:
The parameter that I am passing to do the date comparison in the query above clinical_data.event_date >= ?
, is set as:
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文档),当使用SQlite时,SQLAlchemy自己的DateTime和相关类型提供了日期格式和解析功能……
The SQLAlchemy documentation for SQLite states (see SQLAlchemy SQLite documentation) that "SQLAlchemy’s own DateTime and related types provide date formatting and parsing functionality when SQlite is used..."
我既不问如何将我的字符串对象转换为python datetime对象,也不问该错误意味着什么。我不确定到底为什么当所有东西都定义得足够好时,我仍然不断收到此错误。
I am not asking how to convert my string object to a python datetime object, nor am I asking what the error means. I am not sure exactly why I keep getting this error when everything appears to be sufficiently defined.
EDIT
EDIT
请注意,当我在 event_date $ c $的模型中使用
DateTime
时c>属性,出现以下错误 SQLite DateTime类型仅接受Python datetime和date对象作为输入。
为此,我定义了 valdate = datetime.strptime( '1776-01-01 00:00:00',%Y-%m-%d%H:%M:%S)
而没有 date()
方法。如预期的那样,在这种情况下, type(valdate)
产生< type'datetime.datetime'>
。
Note that when I use DateTime
in my model on the event_date
attribute I get the following error SQLite DateTime type only accepts Python datetime and date objects as input.
For this I define valdate = datetime.strptime('1776-01-01 00:00:00', "%Y-%m-%d %H:%M:%S")
without the date()
method. As expected, type(valdate)
in this case yields <type 'datetime.datetime'>
.
我尝试了将变量 valdate
与 event_date $ c $一起创建的所有组合c>我班的属性。
I have tried every combination of creating the variable valdate
with the event_date
attribute of my class.
推荐答案
错误提示:
SQLite Date类型仅接受Python日期对象作为输入。
SQLite Date type only accepts Python date objects as input.
但是您没有 date
对象-您有一个 datetime
对象!要将您的日期时间
转换为日期
,只需调用其 date()
方法-尽管根据事实判断您的 strptime
格式包含时间字段(%H:%M:%S
),您可能需要更改 event_date
从 Column(Date)
到 Column(DateTime)
But you don't have a date
object — you have a datetime
object! To turn your datetime
into a date
, just call its date()
method — though judging by the fact that your strptime
format includes time fields (%H:%M:%S
), you might want to change event_date
from Column(Date)
to Column(DateTime)
instead.
这篇关于SQLAlchemy的Python数据时间对象问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!