Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is sqlite last_insert_rowid atomic?

I use node.js sqlite3 to manipulate data. I use these codes to insert data to database and get inserted id:

db.run("INSERT INTO myTable (name) VALUES ('test')");
db.get("SELECT last_insert_rowid() as id", function (err, row) {
     console.log('Last inserted id is: ' + row['id']);
});

I think this is not stable. my db connection is always open. when my server serves this code on multiple and same time connections from clients, DoesSELECT last_insert_rowid() get id rightly? Is sqlite last_insert_rowid atomic? thanks.

like image 279
hahamed Avatar asked Mar 16 '15 08:03

hahamed


1 Answers

try{

    db.run("INSERT INTO TABLE_NAME VALUES (NULL,?,?,?,?)",data1,data2,data3,data4,function(err){
                    if(err){
                        callback({"status":false,"val":err});
                    }else{
                        console.log("val  "+this.lastID);
                        callback({"status":true,"val":""});
                    }
                });
}catch(ex){
    callback({"status":false,"val":ex});
}

this.lastID return the last inserted row id By documentation sqlite3 Database#run(sql, [param, ...], [callback])

like image 82
Sachin Jain Avatar answered Sep 29 '22 13:09

Sachin Jain