Skip navigation
JeffActive
Currently Being Moderated

Air 2.0/Flash/sqlite/blobs - not working - code copied...

Jan 30, 2011 9:28 AM

I'm having an impossible time with sqlite/blob fields in an Air 2.0 app.  Nothing I can do is getting them to load properly.  The documentation is very light about how to read them in.  Multiple unanswered threads exist in these forums showing the typical RangeError problem which appears to be fixed by a CAST in the SQL statement.


Has anyone ever read a blob into Air 2.0/Flash?


Code follows with comments showing results and issues...



var sqlFile:File;

var sqlConn:SQLConnection;

var sqlStatement:SQLStatement;

init();


function init():void

{

     sqlConn = new SQLConnection();

     sqlConn.addEventListener(SQLEvent.OPEN, connOpenHandler);

     sqlConn.addEventListener(SQLErrorEvent.ERROR, errorHandler);


     sqlFile = new File(File.desktopDirectory.nativePath);

     sqlFile = sqlFile.resolvePath("sample_with_blob.sqlite");

     sqlConn.open(sqlFile);

}


function connOpenHandler(event:SQLEvent):void

{

     sqlStatement = new SQLStatement();

     sqlStatement.sqlConnection = sqlConn;

     sqlStatement.addEventListener(SQLErrorEvent.ERROR, errorHandler);

     readDB();

}


function readDB()

{

     sqlStatement.addEventListener(SQLEvent.RESULT, selectResultHandler);


          // Note that this CAST to a ByteArray is needed or else a RangeError error is given

          // whenever a blob field is accessed

     sqlStatement.text = "SELECT CAST(blob_field AS ByteArray) as d FROM blob_table WHERE item=0;";

     sqlStatement.execute();

}


function selectResultHandler(event:SQLEvent):void

{

     sqlStatement.removeEventListener(SQLEvent.RESULT, selectResultHandler);

 

     var result:SQLResult = sqlStatement.getResult();

 

     if (result.data != null) {

          var numRows:int = result.data.length;     // returns 1

          var row:Object = result.data[0];          // returns the record with a "d" property

          var ba:ByteArray = ByteArray(row.d);      // errors because row.d is a numeric 0

     }

}



I'm assuming that CAST is needed but I'm not totally sure that ByteArray is the type that should be used.  If the CAST is not used, a RangeError is produced.  I changed the CAST to String and did receive the first part of the blob field but it was immediately truncated when it encountered a binary null.

 

 

What am I doing wrong?  Why isn't this CAST requirement for blobs documented anywhere?  Does this perhaps work with v2.5?

 

 

Thanks for any suggestions.

 

 
Replies
  • Chris Campbell
    9,455 posts
    May 4, 2010
    Currently Being Moderated
    Jan 31, 2011 2:12 PM   in reply to JeffActive

    Hi Jeff,

    Thanks for posting the solution that worked for you!  You caught us on a Sunday, so I didn't see your post here till today.

     

    Question for you, did you create your database outside of AIR?  AIR has actually supported ByteArrays in sqlite since AIR 1.0 but we only officially supported those created by AIR.  As you noted however, AIR 2.5 did add cast support for converting BLOB data to AS ByteArrays.  Here's what the docs have to say:

     

    Starting with AIR 2.5, the SQL CAST operator is supported when reading to convert BLOB data to ActionScript ByteArray objects. For example, the following code reads raw data that is not stored in the AMF format and stores it in a ByteArray object:

     

    stmt.text = "SELECT CAST(data AS ByteArray) AS data FROM pictures;"; 
    stmt.execute(); 
    var result:SQLResult = stmt.getResult(); 
    var bytes:ByteArray = result.data[0].data;

     

    Thanks,

    Chris

     
    |
    Mark as:
  • Chris Campbell
    9,455 posts
    May 4, 2010
    Currently Being Moderated
    Jan 31, 2011 5:24 PM   in reply to JeffActive

    I was working on Sunday, but I don't think house work is what you meant   I agree it would have been helpful if this was in the "what's new" section.  I apologize for that.  We've been working on answering as many posts as possible in a few of the AIR forums (Installations, Performance, and Problems and Bugs) and while we're doing better than the past, our answer percentage is still not as high as we want to be.  Our development forums are more community driven but we also have employees monitoring and contributing there as well.  Either way, always feel free to bump a post if we haven't responded or just shoot me an email at ccampbel@adobe.com and I'll be happy to take a look.

     

    Thanks again,

    Chris

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points