1 Reply Latest reply on Aug 17, 2008 5:41 PM by Dr. Fred Mbogo

    How to save object to SQLite?

    mark416 Level 1
      Hi,

      I am confusing with this.

      How to save object (instance of class) to SQLite and read the object back to Flex?

      Thanks

      Mark
        • 1. Re: How to save object to SQLite?
          Dr. Fred Mbogo Level 1
          SQLite is a relational database, not an object-oriented database, so you can't just spit an object into it without translation. You could serialize it with JSON or XML and store that in a column, but then you're giving up most of the benefit of a proper database engine. It's best to flatten the object yourself into the database structure. How you do that, exactly, depends on your object structure(s).

          So, if you have an object like this:

          var foo:Object = { bar: 'qux', bletch: new Date(), farbly: [ 42, 69 ] };

          you probably need two tables to represent it: a main "foos" table (a collection of foo objects) and a separate "farblys" table to hold the array elements. This separation of arrays out into a table of their own linked to the original table is called normalization.

          Normalization will be covered in any decent tutorial book on SQL databases. You'll need to get one to best understand how to utilize SQLite, or anything like it. I got a lot out of The Practical SQL Handbook. I doubt that it covers SQLite specifically. I have an older edition, so I can't check, but the current edition was released about the same time that SQLite was, and SQLite took some time to get noticed. The SQLite Documentation will fill in the gaps. The main thing to beware of is that SQLite doesn't support all the advanced things you will read about in a general SQL book.

          If you don't want to mess with all this object-relational mapping and normalization stuff, and you don't have much data to store, you might look into Flash Local Stored Objects (LSO), also called "Flash cookies". The runtime has a default limit of 100 KB of data per application. The advantage of LSOs is that you can use them like any other ActionScript object, and Flash handles the storage details for you. It's great for configuration data and small bits of user data.