Cannot get imagedata from database field into image

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Cannot get imagedata from database field into image

Post by mrcoollion » Sun Dec 18, 2016 8:46 pm

I cannot seem to get imagedata from database field into image.
The image just does not show anything.
I saved the data of the image as

Code: Select all

put base64encode(image "GroupLogo") into tBase64ImgData
To make sure I get the data undamaged I used revDatabaseColumnNamed but stil no succes ?

Code: Select all

put "SELECT * FROM "& TableName&" WHERE GroupID =" & tGroupID & ";" into tSQLString
         put revQueryDatabase(DatabaseID, tSQLString) into tRecordSet
         get revDatabaseColumnNamed(tRecordSet, "GroupID", tGroupID)
         get revDatabaseColumnNamed(tRecordSet, "Logo", tLogoData)
         get revDatabaseColumnNamed(tRecordSet, "Name", tGroupName)
         get revDatabaseColumnNamed(tRecordSet, "Status", tGroupStatus)
         Put tGroupID into theDataA[tGroupID]["GroupID"]
         put tGroupName into theDataA[tGroupID]["GroupName"]
         put tGroupStatus into theDataA[tGroupID]["GroupStatus"]
         breakpoint
         -- Put tLogoData into image "TestImage" of this card
         put base64decode(tLogoData) into tBinaryImageData
         --set the text of image "TestImage" of this card to tBinaryImageData
         put tBinaryImageData into image "TestImage" of this card
Any idea's on what i am doing wrong?

Regards,

Paul

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Cannot get imagedata from database field into image

Post by [-hh] » Mon Dec 19, 2016 12:19 am

You could try

Code: Select all

put base64Encode(the imageData of img "groupLogo") into tLogoData 
and (_after_ setting width and height of img "groupLogo" correctly in case size has changed)

Code: Select all

set imageData of img "groupLogo" to base64Decode(tLogoData)
shiftLock happens

mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Re: Cannot get imagedata from database field into image

Post by mrcoollion » Mon Dec 19, 2016 10:04 pm

Found a solution to my problem. I will explain what I did here so anyone else can benefit.
(LiveCode 8.1.1)

The conclusions (in my case):
1) In my case the field type needed to be of type GRAPHIC or BINARY, BLOB did not work with storage and retrieval of base64encode image data.
2) When I retrieved multiple records of data with below code into tRec there must NOT be image data in there. In my case I was not able to split the data anymore with tab and return delimiters.

Code: Select all

revDataFromQuery(,,DatabaseID,tSQL) into tRec
3) Putting and retrieving image data into/from the database can only be done one record and one field at a time.
Therefore I first generate the new record and then UPDATE it with the image field (column). Build and used the below command for this.
Be aware that the image data has already been converted with 'put base64encode(image "GroupLogo") into tBase64ImgData' and I have 'put tBase64ImgData into DBImageData'.

Code: Select all

command AddImageInDBFieldV1 DatabaseID, TableName, KeyFields, DataArrayKeyFields, DBImageFieldName,DBImageData @OutResult
   set itemdelimiter to ","
   put the number of items of KeyFields  into NumberOfKeys // Must at least be 1 !!
   if NumberOfFields <0 or DBImageFieldName is empty or DatabaseID is empty or TableName is empty or DataArrayKeyFields is empty or DBImageData is empty
   then
      put "Error" into AddResult 
      put "One or more parameters are empty !" into AddMsg 
      exit AddImageInDBFieldV1
   end if
   set itemdelimiter to ","
   put the number of items of KeyFields into nbrofKeyFields
   put 1 into counter
   repeat for nbrofKeyFields times
      put item counter of KeyFields into tFieldname
      if counter is 1 then Put tFieldname &" is '" & DataArrayKeyFields[tFieldname] & "'" into AndString
      if counter > 1 then put AndString &" and " & tFieldname &" is '" & DataArrayKeyFields[tFieldname]  & "'" into AndString
      add 1 to counter 
   end repeat
   put "UPDATE "&TableName&" SET "&DBImageFieldName&" = ' "&DBImageData&"' WHERE "&AndString into tQuery
   revExecuteSQL DatabaseID, tQuery
   put the result into OutResult
end AddImageInDBFieldV1
4) Retrieving the image is (as mentioned) also done per record.

In My case I first get all the records I need without the image data into an array named OutArrayAllRecords , and I know how many records there are (all in another command not shown here). Then I go over each record and retrieve the image "Logo" with 'get revDatabaseColumnNamed(tRecordSet, "Logo", tLogoData) ' and put it into a datagrid array ' theDataA[tGroupID]["GroupLogo"] '. Then I update the data grid. Important to know is that I still need to convert the image data with 'put base64decode(pDataArray["GroupLogo"]) into image "DG_ImageLogo" of me ' in the FillInData section of the data grid behaviour script.

Code: Select all

 put 1 into tCounter
      repeat for OutNbrRecords times
         put OutArrayAllRecords[tCounter]["GroupID"] into tGroupID
         put "SELECT * FROM "& TableName&" WHERE GroupID =" & tGroupID & ";" into tSQLString
         put revQueryDatabaseBlob(DatabaseID, tSQLString) into tRecordSet
         get revDatabaseColumnNamed(tRecordSet, "GroupID", tGroupID)
         get revDatabaseColumnNamed(tRecordSet, "Name", tGroupName)
         get revDatabaseColumnNamed(tRecordSet, "Status", tGroupStatus)
         get revDatabaseColumnNamed(tRecordSet, "Logo", tLogoData)
  --
         Put tGroupID into theDataA[tGroupID]["GroupID"]
         put tGroupName into theDataA[tGroupID]["GroupName"]
         put tGroupStatus into theDataA[tGroupID]["GroupStatus"]
         put tLogoData into theDataA[tGroupID]["GroupLogo"]
         put tCounter+1 into tCounter
         revCloseCursor tRecordSet
      end repeat
      set the dgData of group "DG_ActiveGroups" to theDataA 
Maybe not the best way to do this but after hours and hours of try and error only this worked for me.

Remarks, improvements and ideas are very welcome. I think this is not the fastest way because I need to access a record at least twice for write and read if there is an image field in the record. Running the stack I do not see any lag so it is fine for me.

Regards,

Paul

Post Reply