[Qt-qml] I am trying to use the aggregate SQL functions using SQLite in Javascript, called from QML and running under QtCreator (on a Mac)
bruce.cichowlas at nokia.com
bruce.cichowlas at nokia.com
Fri Sep 10 21:45:15 CEST 2010
Hi!
I am using QML with a database, a bit like the "samegame" example.
I think I have things set up pretty well, because I am able to perform queries like this without a problem:
var query = 'SELECT * FROM ' + table + ' WHERE myfield >= ' + n1
+ ' AND myfield < ' + n2 + ' ORDER BY myfield desc';
db.transaction(function(tx) {
var rs = tx.executeSql(query);
ret = rs;
console.log(ret);
});
console.log("...return count: " + ret.rows.length);
However, I'm trying to figure out how to perform the SQL aggregate functions. I haven't found any examples like this on a website. For instance, following an SQLite example I found here
http://developer.apple.com/library/safari/#documentation/iphone/conceptual/safarijsdatabaseguide/usingthejavascriptdatabase/usingthejavascriptdatabase.html
I wrote:
function showDocCount(db, table) {
var result = "uninitialized";
db.readTransaction(function (t) {
t.executeSql('SELECT COUNT(*) AS c FROM ' + table, [], function (t, r) {
console.log("*A*");
result = r.rows[0].c;
}, function (t, e) {
// couldn't read database
console.log("*B*");
result = '(unknown: ' + e.message + ')';
});
console.log("*C*");
});
console.log("*D*");
return result;
}
function getRecordCount(table){
var db = openDatabaseSync("MyDB", "1.0", "Local Table", 100);
return showDocCount(db, table);
}
...
console.log(getRecordCount('mytablename');
I expected to get back *A* and a record count, or maybe *B* and an SQL error.
Instead I got back:
*C*
*D*
uninitialized
which makes it appear that neither the successful nor the failure function were called.
Maybe I'm missing something easy. In any event, it would be useful to see what a successful usage of something like SELECT COUNT(*) ... or SELECT AVG(*)
Thanks,
Bruce
More information about the Qt-qml
mailing list