Android Question SQLite database, data disappeared

Paulsche

Well-Known Member
Licensed User
Longtime User
My Apps have databases with sqlite,

Now there are always times before that a User
reported that after starting the app no more data are there.
Possibly happens after an Android update, if you could
has changed the structure of the file system.

Are then when start the app, the database files not found
and are found then applied new, they are of course empty.

The question now is, how can I prevent that ?

Here is my code when initializing the database files:
B4X:
    If File.Exists(File.DirDefaultExternal, "staende.db") = False Then                'wenn Zählerstände DB nicht vorhanden
        If SQLstaende.IsInitialized = False Then
            SQLstaende.Initialize(File.DirDefaultExternal, "staende.db", True)        'Zählerstände DB initialisieren
        End If
        SQLstaende.ExecNonQuery("DROP TABLE IF EXISTS Staende")
        SQLstaende.ExecNonQuery("CREATE TABLE staende (ticksdatum TEXT, ...")
    Else
        If SQLstaende.IsInitialized = False Then
            SQLstaende.Initialize(File.DirDefaultExternal, "staende.db", True)        'Zählerstände DB initialisieren
        End If
    End If
 

KMatle

Expert
Licensed User
Longtime User
File.DirDefaultExternal is the App's folder like de.myname.myapp. The data should be safe here. Maybe the user uninstalls the app + data or maybe he has some cleaning tool and deletes the app's data. Even after a fw update (except if the user wipes the phone) your app & data should be kept.

However you could do a backup of the db via mail every x days or so. Why mail? It's easy to handle.
 
Upvote 0

Paulsche

Well-Known Member
Licensed User
Longtime User
Make sure that you initialize the SQL object in Service_Create of the starter service. It will simplify your code.

The database file will not be deleted unless the user uninstalls the application.

ok, i have initialize the DB in Service_Create,
B4X:
Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
Log("Service create !")
    DB_init
End Sub

Sub Service_Start (StartingIntent As Intent)
Log("Service start !")
End Sub

Sub Service_Destroy
Log("Service Destroy !")

End Sub

Sub ServiceStop
End Sub

Sub DB_init
'    Dim appversion As Int
'    Dim packageman As PackageManager

    If File.IsDirectory(File.DirDefaultExternal, "Fotos") = False Then                'wenn Verzeichnis für Zählerfotos nicht vorhanden
        File.MakeDir(File.DirDefaultExternal, "Fotos")                                'Verzeichnis erstellen
    End If
   
    If File.Exists(File.DirDefaultExternal, "zaehler.db") = False Then                'wenn Zähler DB nicht vorhanden
        If SQLzaehler.IsInitialized = False Then
            SQLzaehler.Initialize(File.DirDefaultExternal, "zaehler.db", True)        'Zähler DB initialisieren
        End If
        SQLzaehler.ExecNonQuery("DROP TABLE IF EXISTS Zaehler")
        SQLzaehler.ExecNonQuery("CREATE TABLE Zaehler (ticksdatum TEXT, datum TEXT, zaehlernr TEXT, bezeichnung TEXT, zaehlerart TEXT, anbieter TEXT, faktorkwh TEXT, grundpreis TEXT, ehtpreis TEXT, abschlag TEXT, notiz TEXT, ehtart TEXT, ehtwaehr TEXT, objekt TEXT, stadt TEXT, ehtpreisnt TEXT, zweitarifzaehler INT, datumvon TEXT, datumbis TEXT, schmutz TEXT, niederschlag TEXT, abschlaege INT, sortnr INT, multiplikator TEXT, zaehlerid TEXT, einheiten_id INT)")
    Else                                                                            'Zähler DB ist vorhanden
        If SQLzaehler.IsInitialized = False Then
            SQLzaehler.Initialize(File.DirDefaultExternal, "zaehler.db", True)        'Zähler DB initialisieren       
        End If
    End If

The Problem now ist, when i restore the DB-Files from a Backup,
i must close the databases to copy the files, then i Activity.Finish , that the APP must
restart.
Now, the databases are not reinitialized as the starter service is not running.
how can I force the starter service is running again ?
 
Upvote 0
Top