6
votes

I tried to declared a variable contains of Datetime like this

ts1.departure_date = '2012-03-03 10:10:10'

but then I got this error

StatementError: (exceptions.TypeError) SQLite DateTime type only accepts Python datetime and date objects as input. 

I wonder what is the correct way to declare a variable with datetime format? Thanks in advance

3
Have you tried: ts1.departure_date = datetime(2012,3,3,10,10,10) ? - Jon Clements♦
@Joe Clements: write that as answer, will upvote - knitti
@JonClements I tried like what you said and import datetime but then I got this error TypeError: 'module' object is not callable, any thoughts? thanks - wiko
@wiko: datetime.datetime(2012, 3, 3, 10, 10, 10) then. - Martijn Pieters♦

3 Answers

11
votes

First import the datetime class:

from datetime import datetime

Then create a datetime object and use that to set your attribute:

ts1.departure_date = datetime(2012, 3, 3, 10, 10, 10)
0
votes
    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)
0
votes

Ultra strange error that encountered with SQLAlchemy with both SQLite and Postgres

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