[Gambas-user] Q:how to save a file.png into Postgresql

Rolf Schmidt rolf.frogs at ...221...
Sun Aug 31 11:42:49 CEST 2008


Hi richard


> Public sub Save_Image(ImagePath as string)
>
>  Dim img As Image
>  Dim pictureData As String
>  Dim sql As String
>
>   img = Image.Load(ImagePath)  ' Save temp image as png file
>   tempFile = Temp() & ".png"
>   img.Save(tempFile)
>
>
>   pictureData = File.Load(tempFile) 'reload as a string
>
>
> At this point the pictureData string seems  to be ok.
>
> I Then tried writing to the database:
>
>   sql = "insert into temp_image(piccie)"
>           "values($$"
>     sql = sql & pictureData & "$$)"
>
> and got back this message:
>
> Query failed:ERROR: invalid byte sequence for encoding "UTF8":0x89 HINT:
> This error can also happen if the byte sequence does not match the encoding
> expected by the server, which is controlled  by "client_encoding"

Hope you defined your database field as byta for blob.
Then you have to mask out a lot of byte codes in your picture data.

I did it like this:

PUBLIC SUB SavePic_Click()
  DIM hFile AS Stream
  DIM buffer AS Byte
  DIM picText AS String

  OPEN NameOfPic FOR INPUT AS #hFile
    WHILE NOT Eof(hFile)
      READ #hFile, buffer, lof(hFile)
      IF buffer < 32 OR buffer > 126 THEN
        picText = picText & "\\\\" & Coct(buffer)
      ELSE
        SELECT buffer
          CASE 92    ' mask  "|" char
            picText = picText & "\\\\134"
          CASE 39    ' mask "'" char
            picText = picText & "\\'"
          DEFAULT
            picText = picText & Chr$(buffer)
        END SELECT
      END IF
    WEND
  CLOSE hFile

  query = "insert into table (picci) values ('" & picText & "')"
  ....
END

Hope that helps
Fine regards
Rolf
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20080831/a9a76d0d/attachment.sig>


More information about the User mailing list