Working with BLOBS baby. yeah!

Share your advanced PureBasic knowledge/code with the community.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Working with BLOBS baby. yeah!

Post by Fangbeast »

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

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()
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!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

funny... I didn't know the term 'blob' in connection with databases...
oh... and have a nice day.
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

@Kaeru Gaman: http://www.purebasic.fr/german/viewtopi ... 674#115674

(i know, long time ago) ;-)

Greetings ... Kiffi
Hygge
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

now can you get the PB implimentation of of SQLite to do this? :o
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

pdwyer wrote:now can you get the PB implimentation of of SQLite to do this? :o
i'm afraid, i don't understand your question.

in Native-PB there are no equivalents for

Code: Select all

SQLitePrepare()
SQLiteBind*()
SQLiteStep()
SQLiteFinalize()
so need to include the *.pbi from ts-soft.

Greetings ... Kiffi
Hygge
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

A good thing with the include is just how easy it is to use. PB includes the basic command set for all databases but the extras (as Kiffi pointed out) don't exist so that the PB command set is unified across all databases supported.

Sure, you could import the functions from the library and use prototypes to make your missing commands but mixing pb database commands and imported functions is messy and may lead to more bugs (At least it does for me!!).

Using the *source* wrapper is great as it gives you a coherent command set; all starting with the same starting identifying name "SQLite" to use on it, no confusion.

Some people says that wrappers are bad but if the wrapper is source code, where is the problem? You study it and modify it to suit yourself.

And, if many people use the same wrapper instead of importing their own functions that they specifically need, supporting each other becomes a whole lot easier.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Kiffi wrote:
pdwyer wrote:now can you get the PB implimentation of of SQLite to do this? :o
i'm afraid, i don't understand your question.

in Native-PB there are no equivalents for...
So you do understand and your answer is "No" :P
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

What's a blob like fangbeast doing talking about blobs?

:twisted:
I may look like a mule, but I'm not a complete ass.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

srod wrote:What's a blob like fangbeast doing talking about blobs?

:twisted:
Whis is a bloke with a face like a babboon's backside talking about me being a blob eh?? That's a g-string sniffing tart calling a blob a blob.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Fangbeast wrote:Whis is a bloke with a face like a babboon's backside talking about me ......
I wish!

Wait...... :wink:
I may look like a mule, but I'm not a complete ass.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

srod wrote:
Fangbeast wrote:Whis is a bloke with a face like a babboon's backside talking about me ......
I wish!

Wait...... :wink:
I can't wait damnit!! I am off to steal the g-string you have been sniffing, to stop the vicious lies.

Psss, srod.. did the cheque arrive yet???
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

I prefer the late 80's version of the Blob to the 1950's... Much better camera work!
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Pforzheimer
User
User
Posts: 21
Joined: Thu Sep 25, 2008 7:12 am
Location: Germany
Contact:

Post by Pforzheimer »

Rook Zimbabwe wrote:I prefer the late 80's version of the Blob to the 1950's... Much better camera work!
LOL :lol:
And it was real "software"!

But i like this one, too...


Greetings
Pforzheimer
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

just 4 more pforzheimer and you can edit your profile! 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

Could a moderator lock this topic please? Enough off-topic spam has been generated I think. Makes it harder for new people to find anything actually useful.
Post Reply