Search code examples
actionscript-3airflash-builder

AS3. Store Array in Local SQL Database


I am developing an application in Adobe Flash Builder 4.6 (AIR APP) in which a user fills forms and stores data like name, phone, address (along with some report data) in Local SQL Database. My coding is all Okay but have a just one problem. Along with data, there is an array which I am trying to store on Local SQL Database, but my all attempts have failed. I store array in text datatype in database and when I access the stored array it give an error. Cannot convert "[object Object]" to Array. I dont know what is the problem. Please Help ... Thanks.

I use array with nested array like.. My array is

var ar:Array=["report_name", {label: report_label, number: 5}, [{label: Label1, data: Data1}, {label: Label2, data: Data2}, {label: Label3, data: Data3}]];

Now I store this array in database like.

sqlstatement.text ="INSERT INTO table (id, data) VALUES ('', '"+ar+"')";

In this statement data has TEXT datatype. Now when I access it like

var array:Array = [];
array = Array(sqlstatement.getResult().data[0].data;

It give coercion error.


Solution

  • You get the result you get because SQL query is a basically a string, so it is equal to

    "INSERT INTO table (id, data) VALUES ('', '" + ar.toString() + "')"
    

    where you lose all generic objects {...} and the data structure.

    MESepehr is absolutely correct, what you need is JSON data format, which will work fine for you as long, as you keep only Numbers, ints, Strings, Booleans, Arrays, Objects and nulls there.

    Then, it is as easy as the following.

    Store:

    sqlstatement.text = "INSERT INTO table (id, data) VALUES ('', '" + JSON.stringify(ar) + "')";
    

    Restore:

    var array:Array = JSON.parse(sqlstatement.getResult().data[0].data) as Array;