Page 1 of 2

Persistant data storage for Android

Posted: Mon Nov 20, 2017 9:50 am
by IdeasVacuum
Most apps need at least some persistent data storage. Spider Basic Android needs to support Android Private Files, SD Files and TinyDb.

Re: Persistant data storage for Android

Posted: Mon Nov 20, 2017 11:23 am
by Peter
IdeasVacuum wrote:Most apps need at least some persistent data storage. Spider Basic Android needs to support Android Private Files, SD Files and TinyDb.
CreateFile() with the #PB_LocalStorage - Flag should create persistant files, shouldn't it? (can't test it by myself)

Greetings ... Peter

Re: Persistant data storage for Android

Posted: Thu Nov 23, 2017 11:09 pm
by IdeasVacuum
Hi Peter

Not according to the Help:
#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.

Re: Persistant data storage for Android

Posted: Sat Nov 25, 2017 10:29 pm
by tj1010
I posted code to test this.. Since the last build and since changing phones SB doesn't find any device I have..

Re: Persistant data storage for Android

Posted: Thu Jan 25, 2018 10:24 pm
by IdeasVacuum

Re: Persistant data storage for Android

Posted: Fri Jan 26, 2018 12:18 pm
by tj1010
If memory serves correct every version of Android has even "Administrators" sandboxed with their own jailed(?) folders that are in /Android/data/[app domain string]. This is basically what you r/w too/from with native file API and these file-system plugins(to r/w outside you have to root the phone and use Linux API). I still haven't tested if the code I posted that uses SB flag uses that or browser session storage though. I was going to add native file API read-to-buffer,write-buffer, and directory-table-to-array to my camera and wifi sample using a plugin, but didn't have the free time.

Re: Persistant data storage for Android

Posted: Thu Jun 28, 2018 3:47 am
by flaviofornazier
Hi Guys!

I fully agree with you.

No good news about database apps with SB?

Thank you in advanced!

PS: I'm a PB user, but newbie here.

Re: Persistant data storage for Android

Posted: Thu Jun 28, 2018 9:45 am
by poshu
Using localstorage in a Cordova app is a persistent storage solution.

SB has a SQLite implementation since SB 2.00 (or maybe 2.20?).

Both are working well together, and I have several applications using them ;)

Re: Persistant data storage for Android

Posted: Tue Jul 03, 2018 8:43 am
by flaviofornazier
Thank you very much Mr.Poshu!

I'll look and learn about Cordova Plugin and SB.

Cheers!

PS: Can i ask you if you can share a simple SQLite example please?

Re: Persistant data storage for Android

Posted: Tue Jul 03, 2018 1:46 pm
by poshu
You don't need any plugin for it to work, just ignore people rambling above.
SB is really complete as it is and you'll be able to do much before needing any cordova plugin; most of the requests in this forum are from people who are not even trying.
As long as you learn what a callback is and how to properly write one, you should be fine.

Here is an example based on my usual implementation :

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
On the first time, the DB will be created, and then each time you restart the app, a new entry will be added.
It works in cordova (android/iOS) as well as in your browser.