how put an image in mysql database ??

hi, hi have viewed all old conversation but i haven’t find a working solution.
i have made a webApp with webFileUploader1 and webImageView1
i upload a jpg file , then show this file in webimageview
then i assignto P as picture =webImageView.picture
to put this image in mysqldb i have tryed :

mysqlRecordset.edit
mysqlRecordset.field(“pic”).picturevalue=p
mysqlRecordset.update
and this not work

i have try to use memoryblock like this
2)
dim mb as memoryblock
mb=p.getdata(p.format…jpg,100)
mysqlRecordset.edit
mysqlRecordset.field(“pic”).value=mb (tryed also mysqlRecordset.field(“pic”).stringvalue=mb.string(0,mb.lenght) )
mysqlRecordset.update
and this not work

can anyone help me please…

I don’t use a BLOB but convert an file (any file, Office/Picture/Image whatever) to string and store it in a Stringfield.
To read the picture convert it back to file…

[code]Function FileToString(f as FolderItem) As String
//reads a file as BinaryStream
//and saves as string to be used in string field type
//no need for Blobs and fast

dim b as BinaryStream
dim data as String

b = BinaryStream.Open(f,false)
data = b.Read(b.length)
if b.ReadError then
MsgBox(“Error reading”)
end if
b.close
return data

End Function[/code]

Function StringToFile(data as String, fn as String) As FolderItem dim bs as BinaryStream dim f as Folderitem = GetFolderItem(fn) bs = BinaryStream.Create(f,true) bs.Write(data) bs.Close return f End Function

It is taken from the Real Studio forum

ok thanks Alexander , i saw this soulution in older forum post… but

  1. this requires much cpu job, converting, writing file, reading file, reconverting
  2. if i have a blob field in table i can use this field directly from Report as image…
    i have read that the proprierty recordset.fiel(“pic”).picturevalue work with only realsqldatabase , it is true or not ??
    other soultions ??

I have done much testing on this. It is very fast.
On my standard issue HP laptop it is hardly noticable. Split second. If you
you want to use a blob field there is also conversion needed. Furthermore, in the Examples is a demo on how to do this.

When I get back to my desk I have a similar solution which does not require writing/reading to a file. Ill post later

Do you also store the file name in a separate field for easy restoration? I’ve never had the need for this but it would seem logical.

Right, found it. You need a BLOB field in your database and in my example below my field is called IMG_IMAGE. I wrote my picture to my database in the DropObject event of a canvas but it gives you the general idea:


Sub DropObject(obj As DragItem, action As Integer)
  
  Dim p as Picture
  Dim dbRec as new DatabaseRecord
  
  if obj.FolderItemAvailable = true then
    p = picture.Open (obj.FolderItem)
  end if

  dbRec.PictureColumn("IMG_IMAGE") = p
  
  MyDatabase.InsertRecord("tblImages",dbRec)
  
  MyDatabase.Commit

End Sub

MyDatabase is an SQLiteDatabase

Then to get your picture back from your database:


Sub UpdatePhotoReel(g as Graphics)
  dim rs as RecordSet
  dim N as Integer
  dim p as Picture
  
  rs = MyDatabase.SQLSelect("Select * from tblImages ORDER BY id DESC")
  
  if rs.RecordCount > 0 then
    for N = 0 to rs.RecordCount - 1
      
      p = rs.Field("IMG_IMAGE")
      
[DO WHAT YOU WANT WITH YOUR PICTURE p HERE]

      rs.MoveNext
      
    Next N
  end if
  
  
End Sub

Hope this helps…

What is the maximum file size that you can put into a stringfield?

I suspect it depends on the database type. I would avoid all the conversion to string and use the methods I have specified above and write them as BLOBS…

tnx to all,
if i use crystal report, valentina reports or other designer, the report designer process can blob field like an image…
but only if this field contains a sequence of byte like an imageFile.
if i use pictureToString this convert picture to sequence of byte,but this is convertion ,and the report
designer can’t show this field as image…
the PictureColumn proprierty work with only realsqlDatabase and sqllite but not with mysql db…
how can i put the really sequence of bytes of a picture in a blob filed ??

Just for your informations.

Few years ago, maybe five years. It was was very common to use blob for binary files. The drawback of that:

  1. Incremental backup of disk become huge
  2. database performance went down
  3. Corrupted database made it impossible to retrieve files

So more applications went back to store binary files on disk, split up in folders, and only have a file path stored in the database.

I’ve seen the same problem that John mentioned, so that’s the way we do it now. Only store a file path to where the file is on disk

The drawback of that is that when the file is moved, the link is broken. Besides, and I have seen this in document management systems that use this technique is that when making a backup, every single file must be opened and read which is a huge decrease of the performance of the backup system, sometimes causing the backup procedure to fail within the given time frame or backup window. We’re talking about many, many files here.

Creating a backup of a (single file) database is a continuous byte stream without the opening/close overhead.

this works on Desktop not sure if in WE or not

FUNCTION StringToPicture(data as string) as picture
  '// Convert a String to a picture
  Dim p As picture
  If data="" Then Return Nil
  p=picture.FromData(DecodeBase64(data))
  Return p
END FUNCTION

FUNCTION PictureToString(p as picture) as string
  If p=Nil Then Return ""
  Return EncodeBase64(p.GetData(Picture.FormatPNG),0)
END FUNCTION

[quote=32055:@Alexander van der Linden]The drawback of that is that when the file is moved, the link is broken. Besides, and I have seen this in document management systems that use this technique is that when making a backup, every single file must be opened and read which is a huge decrease of the performance of the backup system, sometimes causing the backup procedure to fail within the given time frame or backup window. We’re talking about many, many files here.

Creating a backup of a (single file) database is a continuous byte stream without the opening/close overhead.[/quote]

I’m not so sure if that is a huge concern if you have been taking this into consideration in your development of your application, where you have your root folder and possible to enter a new location if you change to new file location.

Rethanks to all…
I find another solution for image and blob field on mysql…
I use the. LOAD_FILE(‘xxx.xyz’) , this is mysql internal function, and this work fine for me
I can use blob field
directly from report designer like as image ( but with dynamic contains)
Ciao , jury.

[quote=32058:@Dave S]this works on Desktop not sure if in WE or not

[code]
FUNCTION StringToPicture(data as string) as picture
'// Convert a String to a picture
Dim p As picture
If data="" Then Return Nil
p=picture.FromData(DecodeBase64(data))
Return p
END FUNCTION

FUNCTION PictureToString(p as picture) as string
If p=Nil Then Return “”
Return EncodeBase64(p.GetData(Picture.FormatPNG),0)
END FUNCTION
[/code][/quote]

Works great thanks!