11

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

4
  • 2
    Have you tried: ts1.departure_date = datetime(2012,3,3,10,10,10) ? Commented May 20, 2015 at 8:31
  • @Joe Clements: write that as answer, will upvote Commented May 20, 2015 at 8:32
  • @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 Commented May 20, 2015 at 8:38
  • @wiko: datetime.datetime(2012, 3, 3, 10, 10, 10) then. Commented May 20, 2015 at 8:40

3 Answers 3

17

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)
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

0

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

1 Comment

Your model defines currency as a DataTime type. It wouldn't work if you try to add currency = 'USD' where USD is a string, would it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.