Android Question Duplicate record message

MarcioCC

Member
Licensed User
Longtime User
Good night, I created a PRODUCTS table with sqlite it has the field CODBARRAS primary key, I add this field in a listview.
When adding a duplicate record the android system gives me the message that is attached in this post.
How do I display a msgbox with the message: REGISTRATION HAS ALREADY BEEN ADDED !!
Does anyone have an example ??
 

Attachments

  • Screenshot_2017-07-18-23-55-57.png
    Screenshot_2017-07-18-23-55-57.png
    89.2 KB · Views: 188

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Without the code it gets difficult to know but it seems that your table has an autoincrement field.
In this case you must pass a null value for autoincrement field.

something like this:

B4X:
SQL1.ExecNonQuery2("INSERT INTO tbl VALUES(?,?)", Array As Object(Null,name))

Another possibility: the CODBARRAS field is set to unique and you are including twice.

Like I said above. Without the code it gets difficult.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
There are a couple of ways you could do this:

1) Use a Try-Catch-End Try block to catch the error & then look for the error string using a LastException.Message call:

B4X:
Private Sub registerUser

    Try
        SQL1.ExecNonQuery($"INSERT INTO PRODUCTS VALUES(Null, '${name})'"$)
    Catch
        If LastException.Message.Contains("column CODBARRAS is not unique") Then
            'Of course you should use the MsgboxAsync call below, I'm just too lazy to write all the code for you.
            Msgbox("Duplicate Registration", "Registration has already been added!")
        End If
    End Try

End Sub

2) Check the table first to see if the ID is already there (this is a "nicer" way of doing it because you're not relying on an exception to be triggered):

B4X:
Private Sub registerUser

    Private cur as Cursor = SQL1.ExecQuery($"SELECT * FROM PRODUCTS WHERE CODBARRAS=${id}"$)
    If cur.RowCount = 0 Then
        SQL1.ExecNonQuery($"INSERT INTO PRODUCTS VALUES(Null, '${name})'"$)
    Else
        'Of course you should use the MsgboxAsync call below, I'm just too lazy to write all the code for you.
        Msgbox("Duplicate Registration", "Registration has already been added!")
    End If
End Sub

OR

Private Sub registerUser

    If SQL1.ExecQuerySingleResult($"SELECT * FROM PRODUCTS WHERE CODBARRAS=${id}"$) = Null Then
        SQL1.ExecNonQuery($"INSERT INTO PRODUCTS VALUES(Null, '${name})'"$)
    Else
        'Of course you should use the MsgboxAsync call below, I'm just too lazy to write all the code for you.
        Msgbox("Duplicate Registration", "Registration has already been added!")
    End If
End Sub


Also, you should use a Try-Catch-End Try block in any sub where you think there is a possibility of an exception occurring (actually you should really use them in every sub, but if you're lazy like me you probably won't) - so example 2 above should have used one, but I was too lazy to write it.

- Colin.
 
Last edited:
Upvote 0
Top