SQLite and Images

Hello world

I have a problem with Sqlite and the BLOB data type, and Google was not helpful, because I guess that there is no just enough examples yet...

What I do is the following: I use sqlite to store Images as blobs, as seen here. The table has only two columns, url TEXT and BLOB value

             connection.begin();

              sql = "INSERT INTO blobs (url, value) VALUES (@a, @b)";               sqlStatement.text = sql;

              for (var i:int = 0, n:int = images.length; i < n; i++)             {                 sqlStatement.parameters["@a"] = images[i].url;                    sqlStatement.parameters["@b"] = images[i].data;                   sqlStatement.execute();                   sqlStatement.clearParameters();               }

              connection.commit();

whereas the images [i] .url are strings, data BitmapData.

Now the question is: How can I get the image of the BitmapData again? What I'm trying to do is the following

public function getBlob(url:String = null):Object
        {
            var dp:DataProvider = new DataProvider;
            try {
                connection.begin(); 

                var sql:String;

                if (url)
                {
                sql = "SELECT value FROM blobs WHERE url = @a";
                sqlStatement.text = sql;
                sqlStatement.parameters["@a"] = url;
                }
                else
                {
                    sql = "SELECT * FROM blobs";
                    sqlStatement.text = sql;
                }
                sqlStatement.execute();
                sqlStatement.clearParameters();

                var result:SQLResult = sqlStatement.getResult();
                for each (var obj:Object in result.data){
                    if (obj != null)
                        dp.addItem(obj);
                }

                connection.commit();

            } catch (e:SQLError) {
                alert.title = "Database Error (" + e.errorID + ")";
                alert.message = "Error: " + e.message + "\n" + e.details;
                alert.modalAlpha = 0.1;
                alert.dialogSize = DialogSize.SIZE_SMALL;
                alert.addButton("OK");
                alert.show(IowWindow.getAirWindow().group);
                connection.rollback();
                return null;
            }   

            return dp;
        }

and

var dp:DataProvider = getBlob(_newUrl) as DataProvider;
trace(dp.data[0].value is BitmapData);

but which always returns false: All that it is said that dp.data [0] .value is an object which I can't access

Any tips?

This seems to work, using built bitmapData as byteArray, getPixels, setPixels.

Thank you

MIke

package
{
    import flash.data.SQLConnection;
    import flash.data.SQLResult;
    import flash.data.SQLStatement;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.errors.SQLError;
    import flash.filesystem.File;
    import flash.geom.Rectangle;
    import flash.utils.ByteArray;

    [SWF(width="1024", height="600", backgroundColor="#cccccc", frameRate="30")]

    public class SQLBitmapBlob extends Sprite
    {
        private var sqlDatabase:File;
        private var sqlConnection:SQLConnection;
        private var sqlStatement:SQLStatement;
        private var sqlResult:SQLResult;

        private static const BITMAP_WIDTH:int = 96;
        private static const BITMAP_HEIGHT:int = 96;

        public function SQLBitmapBlob()
        {
            super();

            // Init Database
            sqlDatabase = File.applicationStorageDirectory.resolvePath("dbBlob2.db");
            sqlConnection = new SQLConnection();
            sqlConnection.open(sqlDatabase);
            sqlStatement = new SQLStatement();
            sqlStatement.sqlConnection = sqlConnection;

            // Create the table if it doesn't exist
            sqlStatement.text = "CREATE TABLE IF NOT EXISTS blob (id INTEGER PRIMARY KEY AUTOINCREMENT, picture BLOB)";
            try{sqlStatement.execute();}
            catch (error:SQLError){trace("SQL STATEMENT: " + sqlStatement.text + "\n Error: " + error.details);}

            // Create a random bitmapData
            var bmpData:BitmapData = new BitmapData(BITMAP_WIDTH, BITMAP_HEIGHT, true, 0x000000);
            var bytArray:ByteArray = new ByteArray;
            bmpData.noise(25);
            bytArray.writeBytes(bmpData.getPixels(new Rectangle(0,0, BITMAP_WIDTH, BITMAP_HEIGHT)));

            // Save BitmapData to Database
            sqlStatement.text = "INSERT INTO blob (picture) VALUES (@picture)";
            sqlStatement.clearParameters();
            sqlStatement.parameters["@picture"] = bytArray;
            try {sqlStatement.execute();}
            catch (error:SQLError){trace("SQL STATEMENT: " + sqlStatement.text + "\n Error: " + error.details);}

            // Preform Select on Database to return row
            var loadedBitmapData:BitmapData = new BitmapData(BITMAP_WIDTH, BITMAP_HEIGHT, true, 0x000000);
            sqlStatement.text = "SELECT * FROM blob WHERE id = last_insert_rowid()";
            sqlStatement.clearParameters();
            try{sqlStatement.execute();}
            catch (error:SQLError){trace("SQL STATEMENT: " + sqlStatement.text + "\n Error: " + error.details);}

            // Convert returned data back to bitmapData
            var r:Object = new Object();
            sqlResult = sqlStatement.getResult();
            if (sqlResult.data != null){
                r = sqlResult.data[0].picture;
                loadedBitmapData.setPixels(new Rectangle(0,0, BITMAP_WIDTH, BITMAP_HEIGHT), r as ByteArray);
                trace("Retrievied record id: " + sqlResult.data[0].id + " from db.");
            }

            var bmpDisplay:Bitmap = new Bitmap(loadedBitmapData);
            addChild(bmpDisplay);
        }
    }
}

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for