Page 1 of 1

Loading an Image into a SQL DB via BLOB

Posted: Tue Feb 27, 2024 5:16 am
by tikidays
Need some assitance, spent way too long trying to trouble shoot this, cant find any examples that match what I want to do which is

Load an image from memory into a SQL database (not from file)

The sendImageToDB procedure results in. zero bytes for the column, but no database error is produced. How do I know the update is working?'


Code: Select all

Procedure sendImageToDB(source.s)
  Debug "currentEdit = "+Str(currentEdit)
  *buffer = EncodeImage(#originalImage, #PB_ImagePlugin_PNG) : Debug "*buffer = "+Str(MemorySize(*buffer)/1024)+"KB."
      If *buffer
        SetDatabaseBlob(#SQLDatabase, 0, *buffer, MemorySize(*buffer))
        SQL.s = "update Title set "+source+" = '?' where uid ='"+StringField(GetGadgetText(UIDBox),2,":")+"';"
        If DatabaseUpdate(#SQLDatabase, SQL) : Debug "Blob = "+Str(DatabaseColumnSize(#SQLDatabase, currentEdit)/1024)+"KB" : Else : Debug"ERROR: "+SQL : EndIf 
        FreeMemory(*buffer)
      EndIf
EndProcedure

Procedure getImageFromDB(column.i)
  pictureSize = DatabaseColumnSize(#SQLDatabase, column)
  If pictureSize > 0
    *picture = AllocateMemory(pictureSize)
    GetDatabaseBlob(#SQLDatabase, column, *picture, pictureSize)
    CatchImage(#originalImage,*picture)
    FinishDatabaseQuery(#SQLDatabase)
    FreeMemory(*picture)
  Else
    Debug "ERROR: No image in column "+Str(column)
    ProcedureReturn 0
  EndIf
EndProcedure

Re: Loading an Image into a SQL DB via BLOB

Posted: Tue Feb 27, 2024 7:51 am
by infratec
Please provide a fully working (or not working) example. Maybe with SQLite as database.
I have not the time to do the coding arround snippets for testing something.

Re: Loading an Image into a SQL DB via BLOB

Posted: Tue Feb 27, 2024 8:42 pm
by spikey
DatabaseColumnSize isn't updated after a DatabaseUpdate only a DatabaseQuery (also true for the other DatabaseColumn*). You can check AffectedDatabaseRows() after an update to see how many rows were modified.

If the result of DatabaseUpdate is non-zero, the value of DatabaseError is the empty string and AffectedDatabaseRows() = 1 (in this case), it's reasonable to assume that the update worked properly.

Re: Loading an Image into a SQL DB via BLOB

Posted: Wed Feb 28, 2024 4:09 am
by tikidays
spikey wrote: Tue Feb 27, 2024 8:42 pm DatabaseColumnSize isn't updated after a DatabaseUpdate only a DatabaseQuery (also true for the other DatabaseColumn*). You can check AffectedDatabaseRows() after an update to see how many rows were modified.

If the result of DatabaseUpdate is non-zero, the value of DatabaseError is the empty string and AffectedDatabaseRows() = 1 (in this case), it's reasonable to assume that the update worked properly.
Thank you so much, this explains so much to me, also why examples include the file size and title. I thought I was being clever :D