Android Question SQL and blobs

raphipps2002

Active Member
Licensed User
Longtime User
I copied this code from the tutorial on SQL. My aim is to take a picture with the camera have a very simple database with two columns (name Text, image BLOB). Cant quiet tell what is wrong but reading back the file from the sql database cause my app to stop...I think the syntax is wrong, but not sure why, when perhaps saving to and reading from the database. Perhaps someone can tell me where i am going wrong please.

I mean in the SQL write and read statements...

Dim blobfilename as string.....contains the file name of my camera jpg



B4X:
Sub InsertBlob

    'convert the image file to a bytes array
    Dim InputStream1 As InputStream

    InputStream1 = File.OpenInput(File.DirDefaultExternal, BlobFileName)
   
    Dim OutputStream1 As OutputStream
   
    OutputStream1.InitializeToBytesArray(1000)
   
    File.Copy2(InputStream1, OutputStream1)
   
    Dim Buffer() As Byte 'declares an empty array
   
    Buffer = OutputStream1.ToBytesArray
   
    'write the image to the database



    SQL1.ExecNonQuery2("INSERT INTO Photos VALUES(?, ?)", Array As Object(BlobFileName, Buffer))


End Sub




Sub ReadBlob

 
    Dim Cursor1 As Cursor
    'Using ExecQuery2 is safer as it escapes special characters automatically.
    'In this case it doesn't really matter.
   
    Cursor1 = SQL1.ExecQuery2("SELECT image FROM Photos WHERE name = ?", Array As String(BlobFileName))
   
    Dim Buffer() As Byte 'declare an empty byte array
       
    Cursor1.Position = 0
   
    Buffer = Cursor1.GetBlob("image")
   
    Dim InputStream1 As InputStream
   
    InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
   
    Dim Bitmap1 As Bitmap
   
    Bitmap1.Initialize2(InputStream1)
 
    InputStream1.Close
   
   

    Panel1.SetBackgroundImage(Bitmap1)
   

   

End Sub
 

raphipps2002

Active Member
Licensed User
Longtime User
Error message...is "Unfortunately, 'app naome' has stopped" (app name is my app name of course)...

Option to click ok...on doing so app returns to previous activity

however, I noticed that the the SQL statement to insert a photo:-

B4X:
    SQL1.ExecNonQuery2("INSERT INTO Photos VALUES(?, ?)", Array As Object(BlobFileName, Buffer))

Returns a -1 for cursor1 position when trying to find it in the Readblob sub...so was it written or cannot be read??

B4X:
Cursor1 = SQL1.ExecQuery2("SELECT image FROM Photos WHERE name = ?", Array As String(BlobFileName))

I did try a few permutations, but just guess work really e.g. in write Array As Object(BlobFileName, Array as object(Buffer)))..

In SQL manager app (which reads a SQL db) at least i could see for table written to. If I do the former the SQL manager actually crashed too....So I suspect the writing is wrong somehow :(

....However, the KeyValueStore looks interesting...will look at it in detail

thanks for your help
 
Upvote 0
Top