Loading an Image into a SQL DB via BLOB

Just starting out? Need help? Post your questions and find answers here.
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Loading an Image into a SQL DB via BLOB

Post 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
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Loading an Image into a SQL DB via BLOB

Post 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.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Loading an Image into a SQL DB via BLOB

Post 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.
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Re: Loading an Image into a SQL DB via BLOB

Post 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
Post Reply