Android Question App Failed to Intialize Database After B4a 9.9 Crash


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:

    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

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

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
        If sqlDB.IsInitialized = False Then
            sqlDB.Initialize(dbFolder, dbFilename, False)
        End If
        #If LOGGING_ON
        #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


Active Member
Licensed User
Longtime User

- 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;

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(
    at anywheresoftware.b4a.sql.SQL.ExecQuery2(
    at b4a.rTrainer02.dbutils._executememorytable(
    at b4a.rTrainer02.dbutils._executewheel(
    at b4a.rTrainer02.dbfcn._getwheellist(
    at b4a.rTrainer02.main$ResumableSub_init_wheels.resume(
    at b4a.rTrainer02.main._init_wheels(
    at b4a.rTrainer02.main._activity_create(
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(
    at b4a.rTrainer02.main.afterFirstLayout(
    at b4a.rTrainer02.main.access$000(
    at b4a.rTrainer02.main$
    at android.os.Handler.handleCallback(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)
** Activity (main) Resume **
Upvote 0


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:

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
Upvote 0