Android Code Snippet SQLite - check table exists

Alexander Stolte

Well-Known Member
Licensed User
Before we can insert items, we should check, whether the table exists.

Public Sub CheckTableExists(sql As SQL,DBName As String) As Boolean 
    If sql.ExecQuerySingleResult("SELECT count(*) FROM sqlite_master WHERE type='table' AND name ='" & DBName & "'") = 0 Then
        Return False
        Return True
    End If 
End Sub
Last edited:


Staff member
Licensed User
Thank you for sharing. However:
1. It is really unacceptable to build queries like this.
2. The sub name should be CheckTableExists.
3. Better implementation is available in DBUtils (TableExists).
[B4X] DBUtils 2


Licensed User
You should do a db design before you start. So you will know which tables you have. Sometimes it makes sense to create new tables but as a copy of existing ones (e.g. if you have a table for every month or so)