Tuesday, July 15, 2008

Storing Dates in the AIR SQLite Database

One thing that took me awhile to figure out is how to use dates with the SQLite database. It turns out that Adobe modified SQLite so it now supports a DATE data type. This is actually documented, but is a little bit buried. So far as I can tell, it is not in any of the PDF's that provide the standard documentation for flex and air, but only appears in an appendix to the language reference. For complete details, see the "SQL support in local databases" appendix of the ActionScript 3 Language and Components Reference.

In a nutshell, there is a DATE datatype which corresponds to the Date ActionScript class. You can directly insert a date ActionScript object into a database field of the DATE type. Similarly, when you select data from a DATE field of the database, it will be returned as an ActionScript date object. Finally, because the data will come out of the database as a date object, you can then upload it to a remote database, using AMF, and it should correctly map to the date data type on the remote database. I've only done this with SQL Server, but it worked correctly.

3 comments:

Jai said...

Hi,

I am having problems inserting date into SQLite database. I have set the database data type to DATE and then trying to insert the AS date object. I get this error:
Error #3115: SQL Error.', details:'near 'Dec'

Can you please post an example on how to insert the date

David said...

I'm having similar problems, but on the read, it isn't working quite as expected. Somehow the returned Date is "undefined" but the typeof the object is "object", very strange. I wish there was some easy way to verify the data is in the table as expected, I'm much more used to being able to read this information using the MYSQL console or Phpmyadmin.

Any suggestions or working code?

David said...

Here I just found this post which is useful:

http://www.verysimple.com/blog/2008/09/09/working-with-dates-in-flex-air-and-sqlite/