Storing an image into a sqlite table

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Storing an image into a sqlite table

Post by jalz » Thu Sep 11, 2014 8:30 pm

Hi Guys,

I have an image object called logo and I have inserted an image inside of it. I want to press a save button and have that image stored in my sqlite database. I seem to be able to save text based items, but how do I do images. I have a field called logo in the database and it is of blob type.

This is the code I have so far.

Code: Select all

global gConnID

on mouseUp
   # Get the values
   put base64encode(img_logo) into tLogo64
   --put field "fld_logoposition" into tLogoPosition
   put field "fld_footer" into tFooter
   
   put "UPDATE stationary SET "& \
         "logo='" & tLogo64 &"'," & \
         "footer='" & tFooter & "'" &  \
         "WHERE stationaryID=1" into tTheSQLQuery
   
   revExecuteSQL gConnID, tTheSQLQuery
   
end mouseUp
Many thanks

Jalz

Klaus
Posts: 14206
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Storing an image into a sqlite table

Post by Klaus » Thu Sep 11, 2014 9:48 pm

Hi Jalz,

try this:
...
put base64encode(the text of img "logo") into tLogo64
...
Base64Encode will however return a looooooong (text) string, so no need for (binary) BLOB.


Best

Klaus

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Storing an image into a sqlite table

Post by jalz » Thu Sep 11, 2014 10:02 pm

Hi Klaus,

I tried that, but my code doesn't seem to populate the blob field. It has nevertheless populated a footer field which is a text field. If I want to store a logo which is 300x300 px wide, do I store the base64 value in a blob field on a text field?

Code: Select all

   put base64encode(the text of img "img_logo") into tLogo64
   put "insert into stationary(logo, footer) values (:1, :1)" into tSQL
   revExecuteSQL gConnID,tSQL, "*b" & "tLogo64"

Thanks
Jalz

Klaus
Posts: 14206
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Storing an image into a sqlite table

Post by Klaus » Thu Sep 11, 2014 10:19 pm

Looking at the dictionary, I would try:
...
## Since base64encode returns TEXT, I would simply pass the binary data:
put the text of img "img_logo" into tLogo64
put "insert into stationary(logo, footer) values (:1, :1)" into tSQL
revExecuteSQL gConnID,tSQL, "*btLogo64"
## "*b" & "tLogo64"
## maybe concatenating the variable name does not work here!?
...

Best

Klaus

Post Reply