1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| Procedure CreateFileCallback(Status, Filename$, File, SizeRead)
Select Status
Case #PB_Status_Saved
Debug "File saved: " + Filename$ + "(" + SizeRead + " bytes)"
Case #PB_Status_Error
Debug "Can't save the file: " + Filename$
EndSelect
EndProcedure
Procedure PerformQuery(DB)
If DatabaseQuery(DB, "Select * From SuperHeroes Where Prename = 'Peter'")
While NextDatabaseRow(DB)
Debug GetDatabaseString(DB, 1) + "," + GetDatabaseString(DB, 0)
Wend
FinishDatabaseQuery(DB)
Else
Debug "DatabaseQuery() failed: " + DatabaseError()
EndIf
EndProcedure
Procedure ReadFileCallback(Status, Filename$, File, SizeRead)
Select Status
Case #PB_Status_Loaded
Debug "File loaded: " + Filename$
; Get all the file as a new buffer
*Buffer = ExportFileMemory(File)
Debug MemorySize(*Buffer)
DB = OpenDatabase(#PB_Any, *Buffer)
If DB
Debug "OpenDatabase() read from file: OK"
PerformQuery(DB)
EndIf
Case #PB_Status_Error
Debug "Can't read the file: " + Filename$
Debug "Creating a new database"
DB = OpenDatabase(#PB_Any)
If DB
Debug "OpenDatabase(): OK"
DatabaseUpdate(DB, "Create Table SuperHeroes (Prename TEXT, Surname TEXT)")
DatabaseUpdate(DB, "Insert Into SuperHeroes (Prename, Surname) Values ('Peter', 'Parker')")
DatabaseUpdate(DB, "Insert Into SuperHeroes (Prename, Surname) Values ('Peter', 'Jackson')")
DatabaseUpdate(DB, "Insert Into SuperHeroes (Prename, Surname) Values ('Bruce', 'Wayne')")
DatabaseUpdate(DB, "Insert Into SuperHeroes (Prename, Surname) Values ('Clark', 'Kent')")
PerformQuery(DB)
; Now save the database to a persistent file
;
*Buffer = ExportDatabaseMemory(DB)
If CreateFile(0, "mydb.sqlite", @CreateFileCallback(), #PB_LocalStorage)
WriteData(0, *Buffer, 0, MemorySize(*Buffer))
CloseFile(0)
EndIf
CloseDatabase(DB)
Else
Debug "OpenDatabase() failed"
EndIf
EndSelect
EndProcedure
; Try to read the database if already present, or it will create a new one
;
ReadFile(0, "mydb.sqlite", @ReadFileCallback(), #PB_LocalStorage) |
Partager