I'm new to using SQL
The following example has the user load an image, converts the image to a byteArray, and inserts the byteArray in a table named 'images'.
When the user presses a button the data is retrieved from the database.
The problem is retrieving the byteArray from the database, I get Error #1034: Cannot convert object to flash.utils.ByteArray.
Possibly the problem is that I'm SQL illiterate.
import flash.filesystem.File;
import flash.events.Event;
import flash.display.Loader;
import flash.utils.ByteArray;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.data.SQLStatement;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.data.SQLConnection;
import flash.errors.SQLError;
import flash.data.SQLResult;
import flash.data.SQLCollationType;
/* ***************************************
* *
* Establish Connection and make *
* *
* table *
* *
*************************************** */
var conn:SQLConnection = new SQLConnection();
var folder:File = File.applicationStorageDirectory;
var dbFile:File = folder.resolvePath("DBTest.db");
try
{
conn.open(dbFile);
trace("database created");
}
catch(error:SQLError)
{
trace("error message: " + error.message);
trace("details: " + error.details);
}
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = conn;
var sql:String =
"CREATE TABLE IF NOT EXISTS images(" +
"imageID BLOB" +
")";
stat.text = sql;
try
{
stat.execute();
trace("Table Created");
}
catch(error:SQLError)
{
trace("Error Message: " + error.message);
trace("Error Details: " + error.details);
}
var bytes:ByteArray = new ByteArray();
/* ****************************
* *
* Create Button *
* *
**************************** */
var but:Sprite = new Sprite();
but.graphics.beginFill(0x0000dd);
but.graphics.drawRoundRect(400,30,40,40,10,10);
but.graphics.endFill();
addChild(but);
but.addEventListener(MouseEvent.CLICK, gogo);
function gogo(event:MouseEvent):void
{
out();
}
/* **************************
* *
* Load Image *
* *
************************** */
var file:File = File.documentsDirectory;
file.addEventListener(Event.SELECT, handleSelectPicture);
file.browseForOpen("Select Picture");
function handleSelectPicture(event:Event):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, handleLoadPicture);
loader.load(new URLRequest(event.target.url));
}
function handleLoadPicture(event:Event):void
{
// load bitmap
var loader:Loader = Loader(event.target.loader);
var image:Bitmap = Bitmap(loader.content);
// get Bitmap data
var imageData:BitmapData = new BitmapData(image.width,image.height,false);
imageData.draw(image);
var aa:Number = new Number();
aa = image.height / image.width;
var image3:Bitmap = new Bitmap(imageData);
image3.width = 200;image3.height = 200 * aa;
addChild(image3);
// convert to ByteArray
var rect:Rectangle = new Rectangle(0,0,image.width,image.height);
var bytes:ByteArray = imageData.getPixels(rect);
/* ************************
* *
* Insert Image *
* Into Table *
* *
************************ */
var statIn:SQLStatement = new SQLStatement();
statIn.sqlConnection = conn;
var sql2:String =
"INSERT INTO images(imageID) " +
"VALUES ('bytes')";
statIn.text = sql2;
try
{
statIn.execute();
trace("Image inserted");
}
catch(error:SQLError)
{
trace("Error Message: " + error.message);
trace("Error Details: " + error.details);
}
}
/* ********************
* *
* Select ByteArray *
* From Table *
* *
******************** */
function out():void
{
var statOut:SQLStatement = new SQLStatement();
statOut.sqlConnection = conn;
statOut.text = "SELECT CAST(imageID as ByteArray) FROM images";
statOut.execute();
try
{
var ex:SQLResult = statOut.getResult();
trace(ex.data[0]);
var by:ByteArray = ex.data[8];
trace(by);
}
catch(error:SQLError)
{
trace("Error Message: " + error.message);
trace("Error Details: " + error.details);
}
}
Using SQLlite with Air
Thursday, June 21, 2012
[Action Script] Using SQLlite with Air June,2012
Posted by Bimo Hery Prabowo at 7:13 PM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment