Skip to content Skip to sidebar Skip to footer

Android Sqlite Check If Inserted New Value

I'm working with sqlite. I successfully created database and table. I also wrote code which can insert new values in my table. My code is working perfect, but now I want to show fo

Solution 1:

insert() method returns the row ID of the newly inserted row, or -1 if an error occurred.

Change

db.insert(AddNewPhysicalPerson, null, newValues);

like this

long rowInserted = db.insert(AddNewPhysicalPerson, null, newValues);
if(rowInserted !=-1)
    Toast.makeText(myContext, "New row added, row id: " + rowInserted, Toast.LENGTH_SHORT).show();
else
    Toast.makeText(myContext, "Something wrong", Toast.LENGTH_SHORT).show();

Solution 2:

long result= db.insert(table name, null, contentvalues);
      if(result==-1)
          returnfalse;
           elsereturntrue;

this is good solution for it..

Solution 3:

here is another way:-

I don't know if I am too late for your project but have you thought of .lastrowid?

Here is an example of what I did last_rec=self.cursor.lastrowid so lastrec would have the number of the last row inserted.

Here is the info from sqlite.org

Hope it helps, you , or anyone else who wonders about this.

Kind regards and all the best :¬)

joe

Post a Comment for "Android Sqlite Check If Inserted New Value"