Android Question read and write BLOB text field SQLite

magi6162

Well-Known Member
Licensed User
I can not read and write a blob field of a table in SQLite .
can anyone help me ?
maybe an example ?

thanks
 

derez

Expert
Licensed User
This is how you get a blob from a DB and convert it to a string, where "res" is the result of the query to the DB:
B4X:
Dim Buffer() As Byte 'declare an empty byte array
    Buffer = res.GetBlob("Info")
    Dim InputStream1 As InputStream
    InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    Dim bc As ByteConverter
    Dim st As String = bc.StringFromBytes(Buffer, "UTF8")
    InputStream1.Close
After that you can use File.WriteString to put the string in a file.

To put a blob in the DB :
B4X:
Dim InputStream1 As InputStream
        InputStream1 = File.OpenInput([file directory],[file name])
        Dim OutputStream1 As OutputStream
        OutputStream1.InitializeToBytesArray(1000)
        File.Copy2(InputStream1, OutputStream1)
        Dim Buffer() As Byte 'declares an empty array
        Buffer = OutputStream1.ToBytesArray
Then insert the buffer to the DB as blob.
 
Last edited:

keirS

Well-Known Member
Licensed User
The limits of a text field and a blob field are the same in SQLite. So there is little advantage to storing text in a blob.
 
Top