Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"SELECT MAX(rowid) FROM table" query works only after "SELECT * FROM table" has been called

The below function is in a javascript file. The intent is to return the maximum value in the rowid column of a sqlite database. Currently, it works only after a "Select * from table query" has been run. Why is this?

function lastRecord(){  
db.transaction(function (tx) {
      tx.executeSql('SELECT MAX(rowid) as mr FROM Surveys', [], function(tx, results){
  var maxRowid = results.rows.item(0).mr;
  alert(maxRowid);
});
});
} 

I would appreciate clarification regarding what needs to be loaded to get SELECT MAX(rowid) to show the correct MAX rowid value every time the function is called. What is the standard format (structure) for implementing the MAX function? THANKS.

like image 750
portsample Avatar asked Sep 06 '25 03:09

portsample


1 Answers

Have you tried using an order by and a limit? Something like

Select rowid as mr from surveys order by rowid desc limit 1

might work better for you.

like image 199
Xanatos Avatar answered Sep 07 '25 20:09

Xanatos