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
3 Answers
Reset to default 3Have 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. (TheOFFSET
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.
本文标签:
版权声明:本文标题:javascript - "SELECT MAX(rowid) FROM table" query works only after "SELECT * FROM table" has 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744664985a2618473.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论