Persistant data storage for Android
Posted: Mon Nov 20, 2017 9:50 am
Most apps need at least some persistent data storage. Spider Basic Android needs to support Android Private Files, SD Files and TinyDb.
- a basic to master the web -
https://forums.spiderbasic.com/
CreateFile() with the #PB_LocalStorage - Flag should create persistant files, shouldn't it? (can't test it by myself)IdeasVacuum wrote:Most apps need at least some persistent data storage. Spider Basic Android needs to support Android Private Files, SD Files and TinyDb.
#PB_LocalStorage: will save the file on client side using its filename when @closefile() is called. This file
could be opened again in another session in the same browser later, but it can be wiped if the
user clear its local cache. It's also domain related, so if the application domain name change,
the files won't be accessible anymore.
Code: Select all
#LocalDatabase = 0
Procedure Handler_Database_Loading(Status, Filename.s, File, SizeRead)
Protected *buffer
If Status = #PB_Status_Loaded ;The sqlite database file is ready, we now need to load it as a database
*Buffer = ExportFileMemory(File)
CloseFile(0)
If Not OpenDatabase(#LocalDatabase, *Buffer)
FreeMemory(*Buffer)
Debug DatabaseError()
Else
FreeMemory(*Buffer)
Debug "Database Loaded!"
Debug "Here is every time this database was accessed : "
DatabaseQuery(#LocalDatabase,"Select * FROM Example") ; List all the items in the database.
While NextDatabaseRow(#LocalDatabase)
Debug FormatDate("%mm/%dd/%yyyy - %hh:%ii:%ss", GetDatabaseLong(#LocalDatabase,0) ) ; display each entry
Wend
FinishDatabaseQuery(#LocalDatabase)
; now, let's add a new entry :
DatabaseUpdate(#LocalDatabase,"INSERT Into Example (Date) Values ("+Date()+");")
; and don't forget to save it!
*Buffer = ExportDatabaseMemory(#LocalDatabase)
CreateFile(0,"mydbfile",0,#PB_LocalStorage)
WriteData(0,*Buffer,0,MemorySize(*Buffer))
CloseFile(0)
FreeMemory(*Buffer)
EndIf
ElseIf Status = #PB_Status_Error ; The sqlite database file wasn't found, it's probably the first time your user load the app!
; In this case, there is two option : load a default database, or create it from scratch. Here I'll use the second :
OpenDatabase(#LocalDatabase)
DatabaseUpdate(#LocalDatabase,"CREATE TABLE `Example` ( `Date` INTEGER );")
Debug "Database Created!"
; Now, I'll add a single entry to the db :
DatabaseUpdate(#LocalDatabase,"INSERT Into Example (Date) Values ("+Date()+");")
; The important thing to understand about SQLite database is : it always stays in memory. In order to make any change permanant, you need to write it to the local storage.
; Depending on the type of application you are writing, you can use different strategy for this : you could write any change down, you could have a timer and saver every X minutes, wait for your user to save manually...
; now, let's save it!
*Buffer = ExportDatabaseMemory(#LocalDatabase)
CreateFile(0,"mydbfile",0,#PB_LocalStorage)
WriteData(0,*Buffer,0,MemorySize(*Buffer))
CloseFile(0)
FreeMemory(*Buffer)
EndIf
EndProcedure
ReadFile(0,"mydbfile",@Handler_Database_Loading(),#PB_LocalStorage) ; read the sqlite database file from the local storage