On 11/09/2010, at 5:45 AM, ext bruce.cichow...@nokia.com wrote:
> 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(*)

Hi,

QML only implements the synchronous portion of the HTML5 Web Database API (you 
can use WorkerScript if you want database access to happen in another thread 
and not block the GUI; see 
http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeglobalobject.html#database-api 
for the database API docs), which is why your callback isn't being called. 
Silently discarding the extra parameters certainly makes this hard to discover 
though -- I've added http://bugreports.qt.nokia.com/browse/QTBUG-13578 for this.

In your above example, if you changed the query to:
var r = t.executeSql('SELECT COUNT(*) AS c FROM ' + table);

then at point C you should be able to access the count as:
r.rows.item(0).c

Regards,
Michael


_______________________________________________
Qt-qml mailing list
Qt-qml@trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-qml

Reply via email to