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()
