Picture and SQLite

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Picture and SQLite

Post by jak64 »

Good morning,
In the following code, I manage to insert an image into a SQLite table but I cannot read it, CatchImage returns me an error! I searched for a long time but couldn't find why it doesn't work...
Can you help me?

Code: Select all

EnableExplicit
UseSQLiteDatabase()

Declare Insert_Picture_Into_Sqlite_Database()
Declare Read_Picture_From_Sqlite()

Global DbPath.s = "picture.db"
Global Db.i
Global Query.s
Global PictureBuffer.i
Global PictureSize.i
Global PicturePath.s = "picture.png"
Global *Picture
Global Catch.i

Procedure Insert_Picture_Into_Sqlite_Database()
If FileSize(DbPath) = -1
  If Not CreateFile(0, DbPath)
    MessageRequester("Error", "Unable to create database: " + DatabaseError())
    End
  EndIf
  CloseFile(0)
EndIf

Db = OpenDatabase(#PB_Any, DbPath, "", "")
If Db = 0
  MessageRequester("Error", "Unable to open the SQLite database:" + DatabaseError())
  End
EndIf

Query = "CREATE TABLE IF NOT EXISTS images (ID INTEGER PRIMARY KEY, picture BLOB)"
If DatabaseUpdate(Db, Query) = 0
  MessageRequester("Error", "Error creating the table: " + DatabaseError())
  End
EndIf

If ReadFile(0, PicturePath)
  PictureSize = Lof(0)
  PictureBuffer = AllocateMemory(PictureSize)

  If PictureBuffer
    ReadData(0, PictureBuffer, PictureSize)
    CloseFile(0)

    Query = "INSERT INTO images (picture) VALUES (?)"

    If DatabaseQuery(Db, Query)
      SetDatabaseBlob(Db, 0, PictureBuffer, PictureSize)

      If DatabaseUpdate(Db, Query) = 0
        MessageRequester("Error", "Error inserting image: " + DatabaseError())
        End
      Else
        MessageRequester("OK", "Image successfully inserted into the SQLite database!")
      EndIf
    Else
      MessageRequester("Error", "Error preparing the query:" + DatabaseError())
      End
    EndIf
  Else
    MessageRequester("Error", "Unable to allocate memory for image.")
    End
  EndIf
Else
  MessageRequester("Error", "Unable to read the image to be inserted.")
  End
EndIf

CloseDatabase(db)
EndProcedure

Procedure Read_Picture_From_Sqlite()
  Db = OpenDatabase(#PB_Any, DbPath, "", "")
  If Db = 0
    MessageRequester("Error", "Unable to open the SQLite database:" + DatabaseError())
    End
  EndIf
  
  Query = "Select picture FROM images where ID = 1"
  DatabaseQuery(Db, Query)
  
  NextDatabaseRow(Db)
  PictureSize = DatabaseColumnSize(Db, 0)
  *Picture = AllocateMemory(PictureSize)
  GetDatabaseBlob(Db, 0, *Picture, PictureSize)
  Catch = CatchImage(#PB_Any, *Picture, PictureSize)
  If Catch = 0
    MessageRequester("Error", "Error CatchImage !")
  EndIf

  CloseDatabase(Db)
EndProcedure

Insert_Picture_Into_Sqlite_Database()
Read_Picture_From_Sqlite()
User avatar
skywalk
Addict
Addict
Posts: 4241
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Picture and SQLite

Post by skywalk »

It works here with:
UsePNGImageDecoder()
And point to an installed png file:
Global PicturePath.s = "C:\PureBasic-x64\Examples\Sources\Data\ToolBar\Copy.png"
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Picture and SQLite

Post by boddhi »

Salut Jak64

Code: Select all

UsePNGImageDecoder()
[EDIT] Oupss, Skywalk was faster :mrgreen:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Picture and SQLite

Post by jak64 »

Hello everyone,
Thanks for the information.

How stupid I am not to have thought of that!
Post Reply