Save and retrieve picture from sqlite Database
Posted: Fri May 30, 2008 11:02 am
Need to save and retrieve picture using Sqlite Db using Purebasic.
Thank for your support.
Thank for your support.
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Datalen.l = GetDatabaseLen(#Database, Column)
*Data = GetDatabaseData(#Database, Column)
Code: Select all
; Save and retrieve picture from SQLite database
; netmaestro, May 2008
; Convert the image file to a base64 string
If ReadFile(0, #PB_Compiler_Home + "examples\sources\data\purebasiclogo.bmp")
*inputbuffer = AllocateMemory(Lof(0))
If *inputbuffer
ReadData(0,*inputbuffer, Lof(0))
*encodedbuffer = AllocateMemory(MemorySize(*inputbuffer) * 1.35)
If *encodedbuffer
encodedlength = Base64Encoder(*inputbuffer, MemorySize(*inputbuffer), *encodedbuffer, MemorySize(*encodedbuffer))
EndIf
CloseFile(0)
FreeMemory(*inputbuffer)
EndIf
EndIf
pic64string.s = PeekS(*encodedbuffer) ; We'll save this in the database
FreeMemory(*encodedbuffer)
UseSQLiteDatabase()
If OpenDatabase(0, ":memory:", "", "")
DatabaseUpdate(0, "CREATE TABLE MyTable (test VARCHAR);")
; Save the string
DatabaseUpdate(0, "insert into MyTable (test) values('" + pic64string+ "')")
; Retrieve the string
DatabaseQuery(0, "SELECT * FROM MyTable")
If NextDatabaseRow(0)
pic64in.s = GetDatabaseString(0, 0)
; Decode it
*decodedbuffer = AllocateMemory(Len(pic64in))
Base64Decoder(@pic64in, Len(pic64in), *decodedbuffer, Len(pic64in))
; Convert it to an image
CatchImage(0, *decodedbuffer)
FreeMemory(*decodedbuffer)
; Look at it..
OpenWindow(0,0,0,ImageWidth(0),ImageHeight(0),"Image Test")
CreateGadgetList(WindowID(0))
ImageGadget(0,0,0,0,0,ImageID(0))
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
CloseDatabase(0)
EndIf
Else
Debug DatabaseError()
EndIf
Code: Select all
;Image Saver And VIEWER PB4.2
;
;Window Constant
;
Enumeration
#Window_0
#Window_1
EndEnumeration
;
;- DATABASE Constants
Enumeration
#Database = 0
#Image = 0
EndEnumeration
;
;- Gadget Constants
Enumeration
#Frame3D_0
#Image_0
#Button_SavePic
#Button_Begin
#Button_Previous
#Button_Next
#Button_End
#String_PicName
EndEnumeration
;
;- Fonts
Global FontID1 ;begin font constant init*********************
FontID1 = LoadFont(1, "Arial", 8)
Global FontID2
FontID2 = LoadFont(2, "Arial", 12,#PB_Font_Bold)
;- Image Plugins
;
;variable
Global StrgPath.s
Global SQLiteDatabaseName.s
Global RowCount.l ;Init Rowcount in a table
Global DBRow.l ;Row counter To move back And forth in the db
Global Ptr.l ;Row Pointer
Global StrgQuery.s ;query's string
Global StrgPic64.s ; encode picture
Global StrgImageFileName.s ; Path and omage name
Global StrgImagename.s ;Image name To save
Global StrgImageNameLenght.l ;lenght of Image path and name with no extension
Global StrgPicName.s ;Picture name from the database
Global StrgPattern.s ;Picture pattern to be save
; you could add up other picture type of files
; by adding more image encoder
; get an image into memory via ReadFile
Procedure.s ExePath() ;To find exe path For database path
ExePath$ = Space(100)
GetModuleFileName_(0,@ExePath$,100)
ProcedureReturn GetPathPart(ExePath$)
EndProcedure
; Save picture name and picture in the database
Procedure SavePic()
UseJPEGImageDecoder()
StrgPattern = "Image Files (*.bmp, *.jpg, *.png, *.tif)|*.bmp;*.jpg;*.png;*.tif"
StrgImageFileName = OpenFileRequester("Choose an image file", "", StrgPattern, 0)
; Convert the image file to a base64 string
ReadFile(0, StrgImageFileName)
If StrgImageFileName <> ""
*inputbuffer = AllocateMemory(Lof(0))
If *inputbuffer
ReadData(0,*inputbuffer, Lof(0))
*encodedbuffer = AllocateMemory(MemorySize(*inputbuffer) * 1.35)
If *encodedbuffer
encodedlength = Base64Encoder(*inputbuffer, MemorySize(*inputbuffer), *encodedbuffer, MemorySize(*encodedbuffer))
EndIf
CloseFile(0)
FreeMemory(*inputbuffer)
EndIf
StrgPic64 = PeekS(*encodedbuffer) ; We'll save this in the database
FreeMemory(*encodedbuffer)
EndIf
; Save the string (IMAGE)
StrgImageName = GetFilePart(StrgImageFileName)
StrgImageNameLenght = Len(StrgImageName)
StrgImageName = Left(StrgImageName,(StrgImageNameLenght - 4))
result = OpenDatabase(0,SQLiteDatabaseName,User$,Password$)
StrgQuery = "INSERT INTO YourTable (Picture_Name,Picture)Values( '" + StrgImageName + "','" + StrgPic64 + "');"
DatabaseUpdate(0,StrgQuery)
CloseDatabase(0)
EndProcedure
Procedure GetPic() ;Retrieve picture from database
; Retrieve the string
UseJPEGImageDecoder()
StrgPic64 = GetDatabaseString(1, 1)
; Decode it
If StrgPic64 <> ""
*decodedbuffer = AllocateMemory(Len(StrgPic64))
Base64Decoder(@StrgPic64, Len(StrgPic64), *decodedbuffer, Len(StrgPic64))
; Convert it to an image
CatchImage(0, *decodedbuffer)
FreeMemory(*decodedbuffer)
EndIf
EndProcedure
Procedure OpenWindow_0()
If OpenWindow(#Window_0, 443, 0, 310, 315, "", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
If CreateGadgetList(WindowID(#Window_0))
ImageGadget(#Image_0, 10, 10, 0, 0, 0)
ButtonGadget(#Button_SavePic, 10, 270, 100, 40, "Save Picture")
ButtonGadget(#Button_Begin, 120, 280, 50, 30, "Begin")
ButtonGadget(#Button_Previous, 170, 280,40, 30, "<")
ButtonGadget(#Button_Next, 210, 280, 40, 30, ">")
ButtonGadget(#Button_End, 250, 280, 50, 30, "End")
StringGadget(#String_PicName,10,238,290,25,"", #PB_String_ReadOnly |#PB_Text_Center |#PB_String_BorderLess)
SetGadgetFont(#String_PicName, FontID2)
EndIf
EndIf
EndProcedure
StrgPath = ExePath() ;initializing StrgPath
SQLiteDatabaseName = "M:\IMAGE A db\DB\FBG.S3DB"
;SQLiteDatabaseName$ = StrgPath + "DB\FBG.s3db" ; ID Database
result = UseSQLiteDatabase() ; opening sqlite library
result = OpenDatabase(1,SQLiteDatabaseName,User$,Password$)
If Result = 0
MessageRequester("","Database / Base de donnée ???") ;Can't find the database
EndIf
result = DatabaseQuery(1, "select count(*) FROM Your DB")
FirstDatabaseRow(1)
RowCount = (Int(GetDatabaseLong(1,0)))-1
result = DatabaseQuery(1, "SELECT * FROM Your DB")
OpenWindow_0() ;Opening main window, waiting For event
UseJPEGImageDecoder()
Repeat
Event = WaitWindowEvent() ; This line waits until an event is received from Windows
WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
GadgetID = EventGadget() ; Is it a gadget event?
EventType = EventType() ; The event type
If Event = #PB_Event_CloseWindow
CloseDatabase(1)
End
ElseIf Event = #PB_Event_Gadget
If GadgetID = #Button_SavePic
SetGadgetText(#String_PicName,"")
SetGadgetState(#Image_0,0)
SavePic()
result = DatabaseQuery(1, "select count(*) FROM Your DB")
FirstDatabaseRow(1)
RowCount = GetDatabaseLong(1,0)
result = DatabaseQuery(1, "SELECT * FROM You DB")
ElseIf gadgetID = #Button_Begin
result = FirstDatabaseRow(1)
If result
DbRow = 1
StrgPicname = GetDatabaseString(1,0)
SetGadgetText(#String_PicName,StrgPicname)
GetPic()
ResizeImage(0, 290, 215)
SetGadgetState(#Image_0,ImageID(0))
EndIf
ElseIf gadgetID = #Button_Previous
Ptr = DbRow-1
FirstDatabaseRow(1)
DbRow = 1
If Ptr > 1
Repeat
NextDatabaseRow(1)
Dbrow = Dbrow + 1
Until DbRow = Ptr
EndIf
StrgPicname = GetDatabaseString(1,0)
SetGadgetText(#String_PicName,StrgPicname)
GetPic()
ResizeImage(0, 290, 215)
SetGadgetState(#Image_0,ImageID(0))
ElseIf gadgetID = #Button_Next
result = NextDatabaseRow(1)
If result
Dbrow = Dbrow + 1
StrgPicname = GetDatabaseString(1,0)
SetGadgetText(#String_PicName,StrgPicname)
GetPic()
ResizeImage(0, 290, 215)
SetGadgetState(#Image_0,ImageID(0))
EndIf
ElseIf gadgetID = #Button_End
If DbRow < Rowcount-1
FirstDatabaseRow(1)
dbRow = 1
Repeat
NextDatabaseRow(1)
DbRow = DbRow + 1
Until DbRow = RowCount
StrgPicname = GetDatabaseString(1,0)
SetGadgetText(#String_PicName,StrgPicname)
GetPic()
ResizeImage(0, 290, 215)
SetGadgetState(#Image_0,ImageID(0))
EndIf
EndIf
EndIf
Until Event = #PB_Event_CloseWindow ; End of the event loop
You do realise that you are replying to a posting that is 8 years old?
Oi!! Stop looking at my fangs, they are shy!Fangs, Fangs, Fangs.
No, guess I didn't. There have been legitimate users before who have necroposted so I never thought about it.You do realize you're talking to a bot.
Not in the outback (Wish I were, it's quieter out there!). Though I took care of the loneliness problem by being an amateur radio operator so now I can talk dingos underwater all over the world:):)I guess some of you lonely fellows way out in the outback are just glad to have someone to talk to!