Monday 15 August 2011

How to read datetime back from sqlite as a datetime instead of string in Python? -


I am using the sqlite3 module in python 2.6.4 to store a datetime in a SQLite database.

It is very easy to insert, because the scalesh automatically converts the string into a string. The problem is, when reading it comes back as a string, but I need to rebuild the original dataet object. How do i do this

If you declare your column with a type of timestamp, / P>

  & gt; & Gt; & Gt; Db = sqlite3.connect (': memory:', detect_types = sqlite3.PARSE_DECLTYPES) ​​& gt; & Gt; & Gt; C = db.cursor ()> gt; & Gt; & Gt; C.execute ('Table Fu (Bar Integer, Falcon Timestamp)') & lt; Sqlite3 Object object at 0x40fc50 & gt; & Gt; & Gt; & Gt; C.execute ('Insert Foo values ​​(?,?)', (23, datetime.datetime.now ())) & lt; Sqlite3.reer object 0x40fc50 & gt; & Gt; & Gt; & Gt; C.execute (choose from 'foo') & lt; Sqlite3 Object object at 0x40fc50 & gt; & Gt; & Gt; & Gt; C.fetchall () [(23, datetime.datetime (2009, 12, 1, 19, 31, 1, 40113))  

Look? Both int (columns for the declining integer) and datetime (columns for the declared timestamp) avoid the intrinsic type of monster trip.


No comments:

Post a Comment