Error - “SQLite DateTime type only accepts Python ” “datetime and date objects as input.”

前端 未结 3 902
面向向阳花
面向向阳花 2020-12-11 20:29

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

<
相关标签:
3条回答
  • 2020-12-11 20:30

    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
    
    0 讨论(0)
  • 2020-12-11 20:40

    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 讨论(0)
  • 2020-12-11 20:42
        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 讨论(0)
提交回复
热议问题