Android Question App Failed to Intialize Database After B4a 9.9 Crash

rgarnett1955

Active Member
Licensed User
Longtime User
Hi All,

Wondered if any of you had this problem.

- Developed and App that connects to an sqLite DB on the internal storage
- Works fine reads and writes to database correctly.
- B4A hangs and I have to restart it.
- Compile and run the program in debug and release, but when it tries to initalise the database the initialisation fails.
- Try all sorts of things to fix it as I am thinking the db has been damaged or hasn't been copied on installation.
- Tried db initialization almost identical to Erel's SQLExample V1:


B4X:
    Public DBFileDir = File.DirDefaultExternal As String

If File.Exists(DBFileDir, DBFileName) = False  Then
        File.Copy(File.DirAssets, DBFileName, DBFileDir, DBFileName)
    End If
    SQL1.Initialize(DBFileDir, DBFileName, True)           
    
    ColLineWidth = 1dip
    RowLineWidth = 1dip
    RowHeight = 30dip
    RowHeight_1 = RowHeight - RowLineWidth
    lblHeight = 30dip
    edtHeight = 50dip   
    TotalHeight = lblHeight + edtHeight + 10dip   
        
    ' initialize the RowID
    RowID.Initialize

The only difference between Erel's and my code is i shown below:

B4X:
Private dbFolder As String                                 'My folder variable
Public DBFileDir = File.DirDefaultExternal As String    'Erel's folder variable




My Code:

My Project's DB Initaliszation:
Sub Class_Globals
    Private dbFilename As String    
    Private dbFolder As String
    Private sqlDB As SQL
End Sub

dbFolder = DBUtils.CopyDBFromAssets (dbFileNameStr)
    If dbFolder.Length = 0 Then ' Return <> 0 if error
        dbFilename = ""
        Return 1
    End If
    
    dbFilename = dbFileNameStr
    
    'Initialise sql object
    'Open the DB
    Try
        If sqlDB.IsInitialized = False Then
            sqlDB.Initialize(dbFolder, dbFilename, False)
        End If
    Catch
        #If LOGGING_ON
        Log(LastException.Message)
        #End If
        ToastMessageShow("Could not open Database resistanceTrainer.sqlite", True)
        Return 2
    End Try
    
    'Check the database
    Dim qryStr As String = "PRAGMA integrity_check;"
    Dim qryOutList As List   = DBUtils.ExecuteMemoryTable(sqlDB, qryStr, Null, 1)
    Dim row() As String = qryOutList.Get(0)
    If row(0).CompareTo("ok") <> 0 Then
        Return 1
    End If
    Return 0


- Restored a previous backup; the backup ran fine
- Updated the restored file with the updated code from the version that failed to run by copying and pasting the text in sections.


Voila!! It works!!

I am not sure why B4A crashed. It does it so infrequently (perhaps twice) that the sequence of actions that causes it is difficult to determine. I think that it occurred after using the Designer, but I can't be sure.

Best regards
Rob
 

rgarnett1955

Active Member
Licensed User
Longtime User
Additional:

- The file I used for the restored version was an exact copy of the one in the build that failed.
- The error is shown below;

B4X:
Logger connected to:  OPPO CPH1701
--------- beginning of main
Copying updated assets files (32)
** Activity (main) Create, isFirst = true **
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
Could not intialise Database
Error occurred on line: 184 (DBUtils)                                              'Caused by failure of initialization
java.lang.RuntimeException: Object should first be initialized.
    at anywheresoftware.b4a.sql.SQL.checkNull(SQL.java:55)
    at anywheresoftware.b4a.sql.SQL.ExecQuery2(SQL.java:222)
    at b4a.rTrainer02.dbutils._executememorytable(dbutils.java:46)
    at b4a.rTrainer02.dbutils._executewheel(dbutils.java:105)
    at b4a.rTrainer02.dbfcn._getwheellist(dbfcn.java:117)
    at b4a.rTrainer02.main$ResumableSub_init_wheels.resume(main.java:743)
    at b4a.rTrainer02.main._init_wheels(main.java:706)
    at b4a.rTrainer02.main._activity_create(main.java:681)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at b4a.rTrainer02.main.afterFirstLayout(main.java:105)
    at b4a.rTrainer02.main.access$000(main.java:17)
    at b4a.rTrainer02.main$WaitForLayout.run(main.java:83)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:179)
    at android.app.ActivityThread.main(ActivityThread.java:5730)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
** Activity (main) Resume **
 
Upvote 0

rgarnett1955

Active Member
Licensed User
Longtime User
Hi Erel,

I had a look at the link and tutorial and found them informative, but I can't see how they are relevant to the problem I had. I set my db directory to a string without the File.Dir... thingy and it worked. The code which had the File.DirDefaultExternal was an example from the forum. I thought it was something you did, but quite possibly it wasn't.

Since looking at the links you gave me I have set my directory to:

B4X:
Public dbFolder = File.DirInternal As String

And this works OK.

My problem was that after the B4A crash the App would not find the sqlite database, yet if I updated the same app restored from backup and brought it up to date by editing the Main program file it worked.

I clean the app prior to running it.

Not to worry.

I have learnt something useful.

Best regards
Rob
 
Upvote 0
Top