Reputation: 6404
I'm trying to get the number of records with QML LocalStorage
, which uses sqlite. Let's take this snippet in account:
function f() {
var db = LocalStorage.openDatabaseSync(...)
db.transaction (
function(tx) {
var b = tx.executeSql("SELECT * FROM t")
console.log(b.rows.length)
var c = tx.executeSql("SELECT COUNT(*) FROM t")
console.log(JSON.stringify(c))
}
)
}
The output is:
qml: 3
qml: {"rowsAffected":0,"insertId":"","rows":{}}
What am I doing wrong that the SELECT COUNT(*)
doesn't output anything?
EDIT: rows
only seems empty in the second command. Calling
console.log(JSON.stringify(c.rows.item(0)))
gives
qml: {"COUNT(*)":3}
Two questions now:
rows
shown as emptyc.rows.item(0)
Upvotes: 3
Views: 482
Reputation: 50550
In order to visit the items, you have to use:
b.rows.item(i)
Where i
is the index of the item you want to get (in your first example, i
belongs to [0, 1, 2]
for you have 3 items, in the second one it is 0
and you can query it as c.rows.item(0)
).
The rows
field appears empty and it is a valid result, for the items are not part of the rows
field itself (indeed you have to use a method to get them, as far as I know that method could also be a memento that completely enclose the response data) and the item
method is probably defined as not enumerable (I cannot verify it, I'm on the beach and it's quite difficult to explore the Qt code now :-)). You can safely rely on the length
parameter to know if there are returned values, thus you can iterate over them to print them out. I did something like that in a project of mine and it works fine.
The properties inside item(0)
have the same names given for the query. I suggest to rewrite that query as:
select count(*) as cnt from t
Then, you can get the count as:
c.rows.item(0).cnt
Upvotes: 6