Android Question SQLite check if table exists

Declan

Well-Known Member
Licensed User
Longtime User
In my Starter, I am checking for a SQLite Database.
If it does not exist, I create the Database (leadguru.db) with "CreateDB"
I then check for a table (user) with "readUser"
If it does nor exist, I must create it.
However I get the following Error:
B4X:
Logger connected to:  Xiaomi MI 4W
--------- beginning of main
Copying updated assets files (8)
*** Service (starter) Create ***
GOT leadguru.db
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
GOT leadguru.db
Error occurred on line: 43 (Starter)
java.lang.RuntimeException: Object should first be initialized.
    at anywheresoftware.b4a.sql.SQL.checkNull(SQL.java:55)
    at anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(SQL.java:244)
    at anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult(SQL.java:234)
    at com.rob.leadguru.starter._readusers(starter.java:196)
    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:348)
    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 anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at com.rob.leadguru.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2944)
    at android.app.ActivityThread.access$2000(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1492)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
** Service (starter) Start **
*** Service (starter) Create ***
GOT leadguru.db
Error occurred on line: 43 (Starter)
java.lang.RuntimeException: Object should first be initialized.
    at anywheresoftware.b4a.sql.SQL.checkNull(SQL.java:55)
    at anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(SQL.java:244)
    at anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult(SQL.java:234)
    at com.rob.leadguru.starter._readusers(starter.java:196)
    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:348)
    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 anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at com.rob.leadguru.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2944)
    at android.app.ActivityThread.access$2000(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1492)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
** Service (starter) Start **
*** Service (starter) Create ***
GOT leadguru.db
Error occurred on line: 45 (Starter)
java.lang.RuntimeException: Object should first be initialized.
    at anywheresoftware.b4a.sql.SQL.checkNull(SQL.java:55)
    at anywheresoftware.b4a.sql.SQL.ExecNonQuery(SQL.java:73)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    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 anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at com.rob.leadguru.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2944)
    at android.app.ActivityThread.access$2000(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1492)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
*** Service (starter) Create ***
GOT leadguru.db
Error occurred on line: 46 (Starter)
java.lang.RuntimeException: Object should first be initialized.
    at anywheresoftware.b4a.sql.SQL.checkNull(SQL.java:55)
    at anywheresoftware.b4a.sql.SQL.ExecNonQuery(SQL.java:73)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    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 anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at com.rob.leadguru.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2944)
    at android.app.ActivityThread.access$2000(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1492)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
*** Service (starter) Create ***
GOT leadguru.db
Error occurred on line: 45 (Starter)
java.lang.RuntimeException: Object should first be initialized.
    at anywheresoftware.b4a.sql.SQL.checkNull(SQL.java:55)
    at anywheresoftware.b4a.sql.SQL.ExecNonQuery(SQL.java:73)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    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 anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at com.rob.leadguru.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2944)
    at android.app.ActivityThread.access$2000(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1492)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)

My Code in Starter:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public csu As CallSubUtils
    Type Message (Address As String, Body As String)
    
    Dim DB As SQL
    Dim DBFileDir As String = File.DirInternal
    Dim DBFileName As String = "leadguru.db"

End Sub

Sub Service_Create
    ' Delay Sub
    csu.Initialize
    
    ' Setup SQL
    If File.Exists(DBFileDir, DBFileName) Then
        LogColor("GOT leadguru.db", Colors.Magenta)
        readUser
    Else
        LogColor("NO leadguru.db", Colors.Magenta)
        CreateDB
    End If
    
    'Firebase
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")

End Sub

Sub CreateDB
    LogColor("Create leadguru.db", Colors.Magenta)
    DB.Initialize(DBFileDir, DBFileName, True)
    
    readUser
End Sub

Sub readUser
    Dim txt As String
    txt="CREATE TABLE IF NOT EXISTS  user (mobileno TEXT)"
    DB.ExecNonQuery(txt) '<----------------------------------Get ERROR HERE

    Dim ret As Int
    ret = DB.ExecQuerySingleResult("SELECT count(*) FROM user")

    If ret <= 0 Then
        LogColor("Table Exists BUT is Empty", Colors.Magenta)
        'Now load Input Dialog in Main
    Else
        LogColor("Table Has: "&ret, Colors.Magenta)
        'If records are = 1, then just go to Main
        
    End If
End Sub



Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 

PdeG

Member
Licensed User
Longtime User
B4X:
qry = $"CREATE TABLE IF NOT EXISTS <table>
        (<id> INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        <column> TEXT)"$
    vSql.ExecNonQuery(qry)
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Many Thanks
Added:
B4X:
    Dim txt As String
    txt="CREATE TABLE IF NOT EXISTS  user (mobileno TEXT)"
    DB.Initialize(DBFileDir, DBFileName, True)
    DB.ExecNonQuery(txt)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can also use DBUtils.TableExists.

 
Upvote 0
Top