Android Question SQLLiteLight2 example SOLVED

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

The SQLLiteLight2 example has a bug and as much as I go over it, Klaus' code appears all good.
The error occurs when I try to delete the most recent record. A few details below:

B4X:
Private Sub DeleteEntry
    Private Query As String
    Private Answ As Int
   
    'ask the user for confirmation
    Answ = Msgbox2("Do you really want to delete " & edtFirstName.Text & " " & edtLastName.Text, "Delete entry", "Yes", "", "No", Null)

    If Answ = DialogResponse.POSITIVE Then            'if yes, delete the entry
        Query = "DELETE FROM persons WHERE rowid = " & Main.RowIDList.Get(Main.CurrentIndex)
        Main.SQL1.ExecNonQuery(Query)                                    'delete the entry
        Main.RowIDList.RemoveAt(Main.CurrentIndex)                            'remove the rowid from the list
        If Main.CurrentIndex = Main.RowIDList.Size - 1 Then            'if the current index is the last one
            Main.CurrentIndex = Main.CurrentIndex - 1                    'decrement it by 1
        End If
        ShowEntry(Main.CurrentIndex)                                        'show the next entry
        ToastMessageShow("Entry deleted", False)    'confirmation for the user
        ShowButtons
    End If
End Sub


Private Sub ShowEntry(EntryIndex As Int)
    Private RowID As Int
   
    If Main.RowIDList.Size = 0 Then             'check if database is empty
        Return                                                    'if the database is empty leave the routine
    End If
   
    If Mode = "Add" Then
        lblRowID.Text = ""
        edtFirstName.Text = ""
        edtLastName.Text = ""
        edtCity.Text = ""
    Else
        Private ResultSet1 As ResultSet
        RowID = Main.RowIDList.Get(EntryIndex)        'get the rowid for the given entry index
        'read the entry with the given ID
        ResultSet1 = Main.SQL1.ExecQuery("SELECT * FROM persons WHERE rowid = " & RowID)
        lblRowID.Text = RowID                                                                    'display the ID
        ResultSet1.NextRow
        edtFirstName.Text = ResultSet1.GetString("FirstName")        'read the value of the FirstName column
        edtLastName.Text = ResultSet1.GetString("LastName")            'read the value of the LasstName column
        edtCity.Text = ResultSet1.GetString("City")                            'read the value of the City column
        ResultSet1.Close                                                                                'close the ResultSet, we don't need it anymore
    End If
    ShowButtons
End Sub

The error logs as this line:
RowID = Main.RowIDList.Get(EntryIndex) 'get the rowid for the given entry index
in the Private Sub ShowEntry(EntryIndex As Int).

The Log in part:

Obviously the RowID goes out of range but i can't fault it.

Any response welcome.

Regards Roger
 

Jorge M A

Well-Known Member
Licensed User
Longtime User
The small problem is in the line where that compares the current index with the size of the list by subtracting 1.
Since the previous line has already been removed from the list of ID's and the size has already been decreased.
B4X:
        Main.SQL1.ExecNonQuery(Query)                                    'delete the entry
        Main.RowIDList.RemoveAt(Main.CurrentIndex)                            'remove the rowid from the list
        If Main.CurrentIndex = Main.RowIDList.Size - 1 Then            'if the current index is the last one
            Main.CurrentIndex = Main.CurrentIndex - 1                    'decrement it by 1
        End If

It is enough to remove the subtraction of 1.

B4X:
        Main.RowIDList.RemoveAt(Main.CurrentIndex)                            'remove the rowid from the list
        'If Main.CurrentIndex = Main.RowIDList.Size - 1 Then            '<<<--- WRONG SIZE if the current index is the last one
        If Main.CurrentIndex = Main.RowIDList.Size  Then            'if the current index is the last one
            Main.CurrentIndex = Main.CurrentIndex - 1                    'decrement it by 1
        End If
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User


Many thanks Jorge M A. This is something I would probably never found, on the plus side it is another step in learning.

Roger
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…