[Solved] Advise Needed - Saving Image to Database

Post

Posted
Rating:
#1 (In Topic #1469)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone

A while ago one of you was kind enough to show me the following code

Code (gambas)

  1. Global.pc = Picture.FromString(File.Load(FilePath))
  2.     Dim Pic As Picture = Global.pc
  3.     Dim hIcon As Picture = Pic.Stretch(Obj.Height - Obj.Font.Height - 6)  ' stretch the icon to button height lss font height less margin.
  4.     Dim p As Picture = New Picture(Obj.W, Obj.H, True) ' make a new picture (use transparent arg to fix qt bug)
  5.     Obj.Text = ""  ' make sure Obj has not got text
  6.         ' use Paint.class to draw the icon and text to the picture
  7.         Paint.Begin(p)
  8.         Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  9.         Paint.Font.Size = 8
  10.         Paint.DrawText(Text, 0, hIcon.Height, Obj.Width, Obj.Font.Height, Align.Center)
  11.         Paint.End
  12.         Obj.Picture = p  ' set the object picture to new pic
  13.         Obj.Visible = True
  14.         Obj.Enabled = True

and this work great when loading images from the local drive (like Icons for onscreen buttons etc but now I have moved onto a section where I need to Display images and save them to a database and display them when they are recalled (these are images for products on a Choice screen)

below is the code I am working with at the moment (and this is experiment and if it needs to change then I can change it no problem)

Code (gambas)

  1. ImageString = Global.pc.ToString("PNG", 100)
  2.                $Query = Null
  3.             $Query &= "update plumenu "
  4.             $Query &= "SET "
  5.             $Query &= "button_image ='" & ImageString & "' "
  6.             $Query &= "where "
  7.             $Query &= "plulistid='" & labSubMenuID.Caption & "' and "
  8.             $Query &= "button_number ='" & labButtonNumber.Caption & "';"
  9.             Global.$DBBackOfficeCon.Exec($Query)  

but when it tries to save to the database i get the following error message

Query failed: you have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''?PNG \001A' at line 1

if I need to use another method of saving and displaying images from a database then I am open to recommendations (i did before use BASE64 to encode my images when I was using VB.net so if someone could tell me how I can encode and decode and display a decoded BASE64 image file that could be the way forward)

I am sure there is a VERY Simple slution for this but at the moment My brain is not coming up with one.

Kind regards to any who are willing to help me
Andy
Online now: No Back to the top

Post

Posted
Rating:
#2
Banned
 Base64()

FromBase64()
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
 Why save an image to a db? Why not save the image in a folder and save the image's location in the db. Db size will be smaller. The downside could be a possibility of a missing image in the folder.

sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#4
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

Base64()

FromBase64()

Thanks for that I had a feeling it was that one but I was not sure how to use them

I assume to save to the database I would use

Dbfeild = Global.pc.tostring(base64)

And to convert back I would use

Global.pc = Picture.FromString(frombase64(dbfeild))

sholzy said

Why save an image to a db? Why not save the image in a folder and save the image's location in the db. Db size will be smaller. The downside could be a possibility of a missing image in the folder.

I don't want the systems to relay on the network as I could have anything from 1 PoS Terminal to 100 PoS Terminals working. I feel using the database would be better as each terminal has its own local copy and works with that (so it never hits the network database server only to pull updates) so if a network goes down the PoS Terminals keep on running
Online now: No Back to the top

Post

Posted
Rating:
#5
Banned
 Close…

Dbfeild = Base64(Global.pc.tostring())
Online now: No Back to the top

Post

Posted
Rating:
#6
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

Close…

Dbfeild = Base64(Global.pc.tostring())

Thank you Bruce I assume Global.pc = Picture.FromString(frombase64(dbfeild)) is correct
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 I don't know how Mariadb stores blobs. However the disadvantage of storing pictures in databases, as previously mentioned is size. All it would take is one person to update this weeks special on bananas with a 7Gpixel photo and blam! Your database refreshes are going to take hours. I had a similar thing happen some years ago when phones only took 1GB pictures but the effect was not pleasant.

b

Online now: No Back to the top

Post

Posted
Rating:
#8
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

thatbruce said

I don't know how Mariadb stores blobs. However the disadvantage of storing pictures in databases, as previously mentioned is size. All it would take is one person to update this weeks special on bananas with a 7Gpixel photo and blam! Your database refreshes are going to take hours. I had a similar thing happen some years ago when phones only took 1GB pictures but the effect was not pleasant.

b

I am telling customers that the MAX size image is 1MB if they go over this then its their own fault i am also saying to cutomer they must use a PNG or jpeg file as they are the smallest sized files via qulity.
Online now: No Back to the top

Post

Posted
Rating:
#9
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 thank you to everyone who advised me on this i now have working images on my EPoS Screen (i had put that off for months lol)

Once again thank you all for the support you have given me over the course of this EPoS project.
Online now: No Back to the top
1 guest and 0 members have just viewed this.