Working with BLOBS baby. yeah!
Posted: Fri Jun 20, 2008 6:26 am
Kiffi has been very patient with me and provided me with an excellent example of adding a blob to a database and then updating it in-place. He gave permission to post his example to me below.
The original source is from ts-soft which is based on an
older code from him and uses the "SQLite3 BaseFunction-Include for Win + Lin + Unicode" includes found in those posts.
http://www.purebasic.fr/english/viewtop ... 707#194707
Note: This is just adding and updating a single blob in a single record database with no other data. I learned enough from this example to add blob handling to my main (current) project that adds normal table data and blobs and updates them all. Wasn't too bad to learn.
Thanks Kiffi!
The original source is from ts-soft which is based on an
older code from him and uses the "SQLite3 BaseFunction-Include for Win + Lin + Unicode" includes found in those posts.
http://www.purebasic.fr/english/viewtop ... 707#194707
Code: Select all
EnableExplicit
XIncludeFile "SQLite3_Include.pbi"
; --> http://www.purebasic.fr/english/viewtopic.php?p=240853#240853
Define.l File, hDB, lStatement
Define.l SizeOfBlob, MemOfBlob
If SQLiteInit() = #False : Debug "Can't initialisize SQLite3" : End : EndIf
; ------------------------------------------
; Reading PureBasicLogo.bmp
; ------------------------------------------
File = ReadFile(#PB_Any, #PB_Compiler_Home + "examples/sources/data/PureBasicLogo.bmp")
If File = #False : Debug "Can't open PureBasicLogo.bmp" : End : EndIf
SizeOfBlob = Lof(File) : MemOfBlob = AllocateMemory(SizeOfBlob)
If MemOfBlob = #False : Debug "Can't allocate memory" : End : EndIf
ReadData(File, MemOfBlob, SizeOfBlob) : CloseFile(File)
; ------------------------------------------
; Open Database (here: in memory)
; ------------------------------------------
hDB = SQLiteOpen(":memory:")
If hDB = #False : Debug "Can't create database" : End : EndIf
SQLiteExecute(hDB, "Create Table tblBlob (fldBlob BLOB)")
; ------------------------------------------
; Insert Blob into table
; ------------------------------------------
lStatement = SQLitePrepare(hDB, "insert into tblBlob (fldBlob) values (?);")
If lStatement
SQLiteBindBlob(lStatement, 1, MemOfBlob, SizeOfBlob)
SQLiteStep(lStatement)
SQLiteFinalize(lStatement)
EndIf
; ------------------------------------------
; checking, if the correct object is in the database
; ------------------------------------------
lStatement = SQLitePrepare(hDB, "Select fldBlob From tblBlob")
If lStatement
SQLiteStep(lStatement)
SizeOfBlob = SQLiteColumnByte(lStatement, 0)
MemOfBlob = SQLiteColumnBlob(lStatement, 0)
If MemOfBlob
File = CreateFile(#PB_Any, "logo.bmp")
If File
WriteData(File, MemOfBlob, SizeOfBlob) : CloseFile(File)
RunProgram("logo.bmp")
EndIf
EndIf
SQLiteFinalize(lStatement)
EndIf
MessageRequester("", "After the picture appears on your screen, click on 'OK' to update the blob with another picture")
; ------------------------------------------
; Reading PureBasic.bmp
; ------------------------------------------
File = ReadFile(#PB_Any, #PB_Compiler_Home + "examples/sources/data/PureBasic.bmp")
If File = #False : Debug "Can't open PureBasic.bmp" : End : EndIf
SizeOfBlob = Lof(File) : MemOfBlob = AllocateMemory(SizeOfBlob)
If MemOfBlob = #False : Debug "Can't allocate memory" : End : EndIf
ReadData(File, MemOfBlob, SizeOfBlob) : CloseFile(File)
; ------------------------------------------
; Update Blob in table
; ------------------------------------------
lStatement = SQLitePrepare(hDB, "update tblBlob set fldBlob = ?;")
If lStatement
SQLiteBindBlob(lStatement, 1, MemOfBlob, SizeOfBlob)
SQLiteStep(lStatement)
SQLiteFinalize(lStatement)
EndIf
; ------------------------------------------
; checking, if the correct object is in the database
; ------------------------------------------
lStatement = SQLitePrepare(hDB, "Select fldBlob From tblBlob")
If lStatement
SQLiteStep(lStatement)
SizeOfBlob = SQLiteColumnByte(lStatement, 0)
MemOfBlob = SQLiteColumnBlob(lStatement, 0)
If MemOfBlob
File = CreateFile(#PB_Any, "logo.bmp")
If File
WriteData(File, MemOfBlob, SizeOfBlob) : CloseFile(File)
RunProgram("logo.bmp")
EndIf
EndIf
SQLiteFinalize(lStatement)
EndIf
SQLiteClose(hDB)
SQLiteEnd()
Thanks Kiffi!