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.

B4X:
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
        Else
        Return True
    End If 
End Sub
 
Last edited:

Erel

Administrator
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
 

KMatle

Expert
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)
 
Top