How to preserve db data after device power reset ?

thepughman

New Member
Licensed User
Longtime User
Hi,

I've read and used Life Cycle and FirstTime at http://www.b4x.com/forum/basic4andr...87-android-process-activities-life-cycle.html

Here's the procedure to explain the fail:
- Installation of my app drops tables, builds the SQLite, fills default data, db data is fresh/default, PASS.
- Close and Re-open the app, all db data is preserved, PASS.
- Power reset the device, open the app, db data is fresh/default, FAIL.

Verified:
- FirstTime = False when re-opening app. Works like a champ.
- FirstTime = True when opening app after power reset. This is my problem.
- db data is preserved after power reset before calling my app.


How to preserve db data after device power reset ?
How to have FirstTime = False every time after initial install ?


Here's the basic structure of my code:

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
ProgressDialogShow("Initializing database...")
SQL1.Initialize(File.DirDefaultExternal, DBFileName, True)
CreateTables
FillTables
ProgressDialogHide
Else
SQL1.Initialize(File.DirDefaultExternal, DBFileName, True)
End If

.... more stuff to initialize

End Sub

Sub CreateTables
SQL1.ExecNonQuery("DROP TABLE IF EXISTS tblCust")
..... repeated for all tables.
SQL1.ExecNonQuery("CREATE TABLE tblCust (" <elements> ")
..... repeated for all tables.
End Sub

Sub FillTables
Uses List of Maps to load data into tables.
End Sub



I have found answers to ALL my questions in this forum, except this one.
This is my first post :*)

Thank-you to everyone !
Kevin
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Try this code.
It checks if the database file already exists and only if it doesn't exist generates a new database.
B4X:
If FirstTime Then
  ProgressDialogShow("Initializing database...")
  SQL1.Initialize(File.DirDefaultExternal, DBFileName, True)
  If File.Exist(File.DirDefaultExternal, DBFileName) = False Then
    CreateTables
    FillTables
    ProgressDialogHide
  end if
Else
  SQL1.Initialize(File.DirDefaultExternal, DBFileName, True)
End If
Best regards.
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
why are you running drop table ? thats what clean up your DB

maybe because create table fail ?
if thats the reason just use
"CREATE TABLE IF NOT EXISTS "
then you do not need to drop the table ever!


you do not care about first time, for that matter
just but the DB initialize in the first time = true
 
Upvote 0

thepughman

New Member
Licensed User
Longtime User
klaus and melamoud,

Good ideas, and both work.

I think I will also use a flag to manually set to force install/fill the db for testing purposes.

Is there a section to read or an established method/routine for installing upgrades on already delivered platforms ??

Thanks, Dankeschön, de agradecimiento, gratias ago-vos,
 
Upvote 0
Top