Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last inserted row id from sqlite in phonegap android

I am creating an app in which i am inserting a data in the table. After inserting the data in the table i want to get the id of that row so i can do further operation depending on it. I tried to use last_insert_rowid() function of sqlite but found no luck. can any one tell which is the best way to get last inserted row id. Any idea is appreciated. Thanks in advance.

like image 228
Neerav Shah Avatar asked Oct 16 '13 10:10

Neerav Shah


People also ask

How do I find the last row ID in SQLite?

SQLite has a special SQL function – last_insert_rowid() – that returns the ID of the last row inserted into the database so getting the ID of a new row after performing a SQL insert just involves executing the last_insert_rowid() command.

How do I find the last inserted record?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL. Insert some records in the table using insert command.


1 Answers

You can get the id of the last insert on a table like this -

tx.executeSql("INSERT INTO profile('name','label','list_order','category') values(?,?,?,?)", 
    [currentProfile.name, currentProfile.label, currentProfile.list_order, currentProfile.category], 
    function(tx, results){
        var lastInsertId = results.insertId; // this is the id of the insert just performed
    }, 
    failCB
)

The results.insertId in WebSQL is similar to mysql_insert_id() in MySQL -

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
like image 62
Ross Avatar answered Sep 18 '22 12:09

Ross