Android Question Reading SQLite3 'Picture' Field

RichardN

Well-Known Member
Licensed User
Longtime User
I have an SQLite 3 table that contains various fields including a field of type 'Picture'. Naturally, trying to load all the fields with 'SELECT * FROM MyTable' defines a huge Cursor object and slows any device down to a crawl so it is to be avoided. Using a cursor object to retrieve a single record dataset and display by the following code works but seems cumbersome:

B4X:
Cursor1 = SQL.ExecQuery("SELECT Picture1 FROM MyTable WHERE rowid = 99")
    Cursor1.Position = 0
   
    Dim Buffer() As Byte
    Buffer = Cursor1.GetBlob("Picture1")

    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)

    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize2(InputStream1)
    InputStream1.Close
    imgPicture.Bitmap= Bitmap1

For more compact code I tried using:

B4X:
    Buffer = SQL.ExecQuerySingleResult("SELECT Picture1 FROM MyTable WHERE rowid = 99")

Unfortunately it throws an exception as the var Buffer cannot accept the Picture field object without defining a Cursor and using the Cursor.GetBlob method.

Is there a correct way of employing SQL.ExecQuerySingleResult() here ?
 

Mahares

Expert
Licensed User
Longtime User
SQL.ExecQuerySingleResult returns a string or a numeric value. You cannot use it as you found out to show a byte array needed for a blob column. In addition, the code you posted in the Cursor1 line should return the result very quickly.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Hi Mahares..... thanks for the reply, unfortunately as I suspected.

I have been programming hand-held devices since the time when memory was sparse and every byte made a difference. I think the same mindset is still good today :)
 
Upvote 0
Top