How to get last inserted row id in sqlite

function insertToProject(cast, pName) { db.execute('INSERT INTO project (cd, pn) VALUES (?,?)', cast, pName); var x = last_insert_rowid(); return x; } 

I am trying to use javascript in a titanium appcelerator. Can someone tell me what I am doing wrong?

+4
source share
2 answers

For this, you can use the lastInsertRowId property of the database object .

You can use as:

 var x = db.lastInsertRowId; 

lastInsertRowId

lastInsertRowId : Number

ID of the last completed row

Please check this link for more details: Titanium.Database.DB

+5
source

You can also do:

  db.transaction(function(tx) { tx.executeSql("INSERT INTO project (cd, pn) VALUES (?,?)", cast, function(tx, res) { var id = res.insertId; }); }); 

Thus, getting the result of a successful insert, and then its insertId property

+1
source

Source: https://habr.com/ru/post/1415493/


All Articles