Android Code Snippet DBUtils: clear table (delete all)

B4X:
'Tests whether the given table exists
Public Sub TableExists(SQL As SQL, TableName As String) As Boolean
    Dim count As Int = SQL.ExecQuerySingleResult2("SELECT count(name) FROM sqlite_master WHERE type='table' AND name=? COLLATE NOCASE", Array As String(TableName))
    Return count > 0
End Sub

'Deletes all the records of a table
Public Sub ClearTable(SQL As SQL, TableName As String)
    If TableExists(SQL, TableName) = False Then
        Return
    End If
    SQL.ExecNonQuery("DELETE FROM " & TableName)
End Sub
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Utility methods or library methods should avoid catching errors (in most cases)
Thanks, Erel, but how to make sure that SQL's NonQuery is done without error ?
 

DonManfred

Expert
Licensed User
Longtime User
but how to make sure that SQL's NonQuery is done without error ?
Use a non query batch and wait for the result.
B4X:
For i = 1 To 1000
  sql.AddNonQueryToBatch("INSERT INTO table1 VALUES (?)", Array(Rnd(0, 100000)))
Next
Dim SenderFilter As Object = sql.ExecNonQueryBatch("SQL")
Wait For (SenderFilter) SQL_NonQueryComplete (Success As Boolean)
Log("NonQuery: " & Success)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Thanks, Erel, but how to make sure that SQL's NonQuery is done without error ?
If the SQL command raised an error then there is a programming mistake that should be fixed. In most cases it is better not to hide the error and let the developer, who is using the code you posted, handle the error in any way they like.
 

peacemaker

Expert
Licensed User
Longtime User
OK, thanks, the 1st post is edited.
 
Top