Android Question Confirmation says ok but row not deleted. OK on s21 phone external db not on s25 Dir.internal

garkin

Member
Licensed User
Longtime User
Sub DeleteEntry
Private Query As String
'''.........................................
SQL1.Initialize(File.DirInternal, "PHockey.db", True)
'''.........................................
Private Answ As Int
'ask the user for confirmation
Answ = Msgbox2("Do you really want to delete " & edtName.Text, "Delete entry", "Yes", "", "No", Null)
If Answ = DialogResponse.POSITIVE Then 'if yes, delete the entry
'''.........................................


Query = "DELETE FROM PHockey WHERE ID = " & Main.IDList.Get(Main.CurrentIndex)
Main.SQL1.ExecNonQuery(Query) 'delete the entry
Main.IDList.RemoveAt(Main.CurrentIndex) 'remove the ID from the list
If Main.CurrentIndex = Main.RowNumber - 1 Then 'if the current index is the last one
Main.CurrentIndex = Main.CurrentIndex - 1 'decrement it by 1
End If
Main.RowNumber = Main.RowNumber - 1 'decrement the row count by 1
ShowEntry(Main.CurrentIndex) 'show the next entry
ToastMessageShow("Entry deleted", False) 'confirmation for the user
ShowButtons
End If
End Sub
 

Alex_197

Well-Known Member
Licensed User
Longtime User
Sub DeleteEntry
Private Query As String
'''.........................................
SQL1.Initialize(File.DirInternal, "PHockey.db", True)
'''.........................................
Private Answ As Int
'ask the user for confirmation
Answ = Msgbox2("Do you really want to delete " & edtName.Text, "Delete entry", "Yes", "", "No", Null)
If Answ = DialogResponse.POSITIVE Then 'if yes, delete the entry
'''.........................................


Query = "DELETE FROM PHockey WHERE ID = " & Main.IDList.Get(Main.CurrentIndex)
Main.SQL1.ExecNonQuery(Query) 'delete the entry
Main.IDList.RemoveAt(Main.CurrentIndex) 'remove the ID from the list
If Main.CurrentIndex = Main.RowNumber - 1 Then 'if the current index is the last one
Main.CurrentIndex = Main.CurrentIndex - 1 'decrement it by 1
End If
Main.RowNumber = Main.RowNumber - 1 'decrement the row count by 1
ShowEntry(Main.CurrentIndex) 'show the next entry
ToastMessageShow("Entry deleted", False) 'confirmation for the user
ShowButtons
End If
End Sub
First of all use parameterized query

B4X:
Dim Query as string

Query="DELETE FROM PHockey WHERE ID = ?"
Main.SQL.ExecuteNonQuery2(Query,ID)

But I would do like this

B4X:
dim Qty as int

'Let's check if the record exists
Query="Select count(ID) as Qty FROM PHockey WHERE ID = ?"

Qty=Main.SQL1.ExecQuerySingleResult2(Query,ID)

If Qty>0 then
    Query="DELETE FROM PHockey WHERE ID = ?"
    Main.SQL.ExecuteNonQuery2(Query,ID)
End If
 
Upvote 0

garkin

Member
Licensed User
Longtime User
First of all use parameterized query

B4X:
Dim Query as string

Query="DELETE FROM PHockey WHERE ID = ?"
Main.SQL.ExecuteNonQuery2(Query,ID)

But I would do like this

B4X:
dim Qty as int

'Let's check if the record exists
Query="Select count(ID) as Qty FROM PHockey WHERE ID = ?"

Qty=Main.SQL1.ExecQuerySingleResult2(Query,ID)

If Qty>0 then
    Query="DELETE FROM PHockey WHERE ID = ?"
    Main.SQL.ExecuteNonQuery2(Query,ID)
End If
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
B4X:
Main.SQL.ExecuteNonQuery2(Query,ID)

The second argument should be a List (or an array that can convert to a List). Try this :

B4X:
Main.SQL.ExecuteNonQuery2(Query, Array as Object(ID))
 
Upvote 0

garkin

Member
Licensed User
Longtime User
Thanks Brian. Still have the problem. I'm not sure why the code i showed does not work with my S25+ phone. It has been ok on my S21 and Tab8.
Both those devices used Dir.External for the hockey db. S25 and api35 want Dir.internal so that is the only difference in the coding
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Thanks Alex but I am getting a compiling error with the word ID
ID stands for your Main.IDList.Get(Main.CurrentIndex)

So you can adjust my code to this


B4X:
Dim ID,Qty As int

ID=Main.IDList.Get(Main.CurrentIndex)

'Let's check if the record exists
Query="Select count(ID) as Qty FROM PHockey WHERE ID = ?"

Qty=Main.SQL1.ExecQuerySingleResult2(Query,ID)

If Qty>0 then
    Query="DELETE FROM PHockey WHERE ID = ?"
    Main.SQL.ExecuteNonQuery2(Query,ID)
End If
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
The code suggested by @Alex_197 will test if you are trying to delete a non-existent record, but it does not fix the problem of why you try to do that in the first place. Here are some more questions.

Does this problem occur (on the S25+) every time that you try to delete an ID, or only in some cases? If so can you describe those cases?

How is your database created at installation time? Do you create a new database, or do you import an existing database? If it is imported how do you know that the import is successful?

B4X:
  If Main.CurrentIndex = Main.RowNumber - 1 Then
    Main.CurrentIndex = Main.CurrentIndex - 1
  End If

This code is a bit suspicious. It looks as though you are trying to maintain a copy of a variable that already exists. It might be nothing, but that sort of procedure is best avoided. Maybe in some other unchecked situation these two values get out-of-step.
 
Upvote 0

garkin

Member
Licensed User
Longtime User
Thanks Brian
To explain further this is my original code based on SQLite2 back a few years ago. Everything is fine except when I go to update, delete, or add a player in each case the response says the job is completed but after going out of the app and back in nothing has changed. On my other devices Tab8 S21+ and note 8 all worked fine. the db was created in db browser for sqlite and was placed outside the app Dir.root.External. Now with s25+ phone api target 35 android 15 and File.DirInternal is the problem.
Code below is for update a player info
Sub UpdateEntry
Private Query As String

Query = "UPDATE PHockey Set Name = ?, Birthdate = ?, Eage = ?, Phone = ?, EMail = ? WHERE ID = " & Main.IDList.Get(Main.CurrentIndex)
Main.SQL1.ExecNonQuery2(Query, Array As String(edtName.Text, edtBirthdate.Text, edtEage.Text, edtPhone.Text, edtEMail.Text))
ToastMessageShow("Entry updated", False)
ShowButtons


End Sub

Thanks
Garth
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Okay - now I am 90% sure that we can fix this problem.

1. I assume that your database file on your new phone is in the same folder location as used on your older phones. If not you need to move it there.

2. Open up the B4A IDE and load your app code. Remove all of your recent changes. If you have kept a backup of your last working level used in your older phones then load that code instead - that is the level that we need to get back to.

3. Open the Manifest Editor from the Project menu. On line 5 change android:targetSdkVersion="35" from "35" to "29".

4. Recompile your project and download it to your new phone.

Your app should now run on your new phone. If it does not then we are in the 10% uncertainty zone - let me know. If everything now works and you want me to explain why then let me know that too.
 
Upvote 0
Top