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

I have been trying this using javascript in titanium appcelerator. Can anybody tell me what I am doing wrong?

John Woo
  • 258,903
  • 69
  • 498
  • 492
nitin
  • 571
  • 1
  • 4
  • 16
  • no errors? some more information would be usefun –  Jun 01 '12 at 01:33
  • 1
    thanks but I got it.. have to use - var x = db.lastInsertRowId; instead of last_insert_rowid(); – nitin Jun 01 '12 at 01:37
  • I know, I'm just asking if there is any errors? What is the `x` value when you run your code? –  Jun 01 '12 at 01:38
  • line = 19; message = "Can't find variable: last_insert_rowid"; Earlier I was getting this error. Now I am getting the x as the last id being inserted. – nitin Jun 01 '12 at 01:47

2 Answers2

5

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

You can use like:

var x = db.lastInsertRowId;

lastInsertRowId

lastInsertRowId : Number

The identifier of the last populated row

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

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
2

You may 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 the successful insert and then its property insertId

luis.ap.uyen
  • 1,314
  • 1
  • 11
  • 29