admin管理员组

文章数量:1390670

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.

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.

Share Improve this question edited Aug 21, 2013 at 5:21 portsample asked Aug 12, 2013 at 22:35 portsampleportsample 2,1224 gold badges22 silver badges43 bronze badges 10
  • 3 You're exposing your database to the world via a scripting language. That screams 'bad idea' to me. – McAden Commented Aug 12, 2013 at 22:41
  • 1 @McAden it's a SQLite database kept in the browser I think; it's like a gigantic cookie :) – Pointy Commented Aug 12, 2013 at 22:45
  • This app will be on small portable devices not connected to the internet. – portsample Commented Aug 12, 2013 at 22:47
  • What's the error/return? – McAden Commented Aug 12, 2013 at 22:59
  • 2 @user2676598 "Security through obscurity, is not security". Always remember that just because it's a hidden Javascript file on a mobile app, doesn't mean your users can't extract it. – RekindledPhoenix Commented Aug 12, 2013 at 23:02
 |  Show 5 more ments

3 Answers 3

Reset to default 3

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.

If you want the maximum row ID from those in a range, you can execute the following:

SELECT MAX(rid) as mr FROM (
  SELECT rowid as rid FROM surveys ORDER BY rowid LIMIT 10000 OFFSET 0
)

How it Works

  • The inner (parenthesised) SELECT gets the row IDs for the specific range. (The OFFSET of a limit dictates which row to start from, where the first row in a table is row 0. Think of it as how many rows to skip, from the start.)
  • The outer SELECT, SELECT MAX(rid), gets the maximum value from that range.

Got it. The problem was HTML related: not a SQL issue.

本文标签: