Persistant data storage for Android

Got an idea for enhancing SpiderBasic? New command(s) you'd like to see?
IdeasVacuum
Posts: 143
Joined: Tue Feb 25, 2014 1:27 pm

Persistant data storage for Android

Post by IdeasVacuum »

Most apps need at least some persistent data storage. Spider Basic Android needs to support Android Private Files, SD Files and TinyDb.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Persistant data storage for Android

Post 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
IdeasVacuum
Posts: 143
Joined: Tue Feb 25, 2014 1:27 pm

Re: Persistant data storage for Android

Post 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.
tj1010
Posts: 201
Joined: Wed May 27, 2015 1:36 pm
Contact:

Re: Persistant data storage for Android

Post by tj1010 »

I posted code to test this.. Since the last build and since changing phones SB doesn't find any device I have..
IdeasVacuum
Posts: 143
Joined: Tue Feb 25, 2014 1:27 pm

Re: Persistant data storage for Android

Post by IdeasVacuum »

tj1010
Posts: 201
Joined: Wed May 27, 2015 1:36 pm
Contact:

Re: Persistant data storage for Android

Post 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.
User avatar
flaviofornazier
Posts: 5
Joined: Thu Jun 28, 2018 2:25 am
Location: Brazil

Re: Persistant data storage for Android

Post 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.
poshu
Posts: 96
Joined: Mon Feb 24, 2014 11:46 pm

Re: Persistant data storage for Android

Post 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 ;)
User avatar
flaviofornazier
Posts: 5
Joined: Thu Jun 28, 2018 2:25 am
Location: Brazil

Re: Persistant data storage for Android

Post 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?
poshu
Posts: 96
Joined: Mon Feb 24, 2014 11:46 pm

Re: Persistant data storage for Android

Post 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.
Post Reply