我试图像这样声明一个包含Datetime的变量
ts1.departure_date = '2012-03-03 10:10:10'
然后我收到了这个错误
StatementError: (exceptions.TypeError) SQLite DateTime type only accepts Python datetime and date objects as input.
我想知道用datetime格式声明变量的正确方法是什么?提前致谢
答案 0 :(得分:7)
首先导入datetime
类:
from datetime import datetime
然后创建一个datetime
对象并使用它来设置属性:
ts1.departure_date = datetime(2012, 3, 3, 10, 10, 10)
答案 1 :(得分:0)
expiration_year = int(form.expiration_date.data[:4])
expiration_month = int(form.expiration_date.data[5:7])
expiration_date = int(form.expiration_date.data[8:10])
expiration_date =datetime(expiration_year,expiration_month,expiration_date)
答案 2 :(得分:0)
使用SQLite和Postgres的SQLAlchemy遇到的超奇怪错误
from datetime import datetime
....
#WORKS
lending_operation = models.LendingOperation(
device_id = device.device_id,
start_using = datetime.now()
)
db.session.add(lending_operation)
db.session.commit()
#DIDN'T WORK
lending_operation = models.LendingOperation(
currency = "USD",
device_id = device.device_id,
start_using = datetime.now()
)
db.session.add(lending_operation)
db.session.commit()
#MODEL: models.py
class LendingOperation(db.Model):
.....
start_using = db.Column(db.DateTime, default=datetime.now )
.....
currency = db.Column(db.DateTime)
Hope it helps, didn't find any info on the exception