SQLite date storage and conversion

后端 未结 2 497
栀梦
栀梦 2020-12-06 10:48

I am having design problems with date storage/retrieval using Python and SQLite.

I understand that a SQLite date column stores dates as text in ISO format (ie.

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 11:18

    If you set detect_types=sqlite3.PARSE_DECLTYPES in sqlite3.connect, then the connection will try to convert sqlite data types to Python data types when you draw data out of the database.

    This is a very good thing since its much nicer to work with datetime objects than random date-like strings which you then have to parse with datetime.datetime.strptime or dateutil.parser.parse.

    Unfortunately, using detect_types does not stop sqlite from accepting strings as DATE data, but you will get an error when you try to draw the data out of the database (if it was inserted in some format other than YYYY-MM-DD) because the connection will fail to convert it to a datetime.date object:

    conn=sqlite3.connect(':memory:',detect_types=sqlite3.PARSE_DECLTYPES) 
    cur=conn.cursor()
    cur.execute('CREATE TABLE foo(bar DATE)')
    # Unfortunately, this is still accepted by sqlite
    cur.execute("INSERT INTO foo(bar) VALUES (?)",('25/06/2003',))
    
    # But you won't be able to draw the data out later because parsing will fail
    try:
        cur.execute("SELECT * FROM foo")
    except ValueError as err:
        print(err)
        # invalid literal for int() with base 10: '25/06/2003'
        conn.rollback()
    

    But at least the error will alert you to the fact that you've inserted a string for a DATE when you really should be inserting datetime.date objects:

    cur.execute("INSERT INTO foo(bar) VALUES (?)",(datetime.date(2003,6,25),))
    cur.execute("SELECT ALL * FROM foo")
    data=cur.fetchall()
    data=zip(*data)[0]
    print(data)
    # (datetime.date(2003, 6, 25),)
    

    You may also insert strings as DATE data as long as you use the YYYY-MM-DD format. Notice that although you inserted a string, it comes back out as a datetime.date object:

    cur.execute("INSERT INTO foo(bar) VALUES (?)",('2003-06-25',))
    cur.execute("SELECT ALL * FROM foo")
    data=cur.fetchall()
    data=zip(*data)[0]
    print(data)
    # (datetime.date(2003, 6, 25), datetime.date(2003, 6, 25))
    

    So if you are disciplined about inserting only datetime.date objects into the DATE field, then you'll have no problems later when drawing the data out.

    If your users are input-ing date data in various formats, check out dateutil.parser.parse. It may be able to help you convert those various strings into datetime.datetime objects.

提交回复
热议问题