0

I am attempting to return all column values from an Sqlite db to a Qchart in qml in Ubuntu SDK - the db contains various fields including kilometres, miles, etc. I would like to return all values in one field (e.g. kilometres) into the Qchart - I know that this is possible using an array, e.g.

function testData(){
var kilometres = ["25","45","42","39"];
    return kilometres;
}

this will happily display the values 25,45,42 & 39 in the chart. However, try as I might, I cannot find a way to pass the values contained in the database to the array shown above. The following is what I have tried so far:-

// Chart Data
function getData() {
    var db = LocalStorage.openDatabaseSync("data.sql", "1.0",  "mileageDataBase", 10000000);
    var res = "";
    db.transaction(
    function(tx) {
    var rs = tx.executeSql('SELECT kilometres FROM mpg');
    res = rs.rows.item().kilometres;
    }
 );
 return res;
 }

(Please note that an earlier version of this question I asked about returning values to a TextField. I thought I could use this TextField to populate the graph but, after some experimentation, that does not appear to be the case. Apologies for the confusion.)

  • Possible duplicate of "[How to use a SQLite database from QML?](http://askubuntu.com/questions/352157/how-to-use-a-sqlite-database-from-qml)" which says `import QtQuick.LocalStorage 2.0`, run requests from QML, and it's not even an Ubuntu-specific library. I personally prefer a different solution: the database is handled in C++ (using libcppdb, libsqlite, whatever), and some 'Model' classes are written to expose data to the QML. – Velkan Nov 21 '15 at 17:42
  • Thanks for the reply. I'm already using that library in QML - where I'm struggling is in returning all values from a column. Using some of the example code in the link you gave in, say, a text field returns the error 'Unable to assign [undefined} to QString' - I probably need to do a lot of reading up on Javascript, tbh, but I was wondering if there was a quick and easy way of doing this. – Michael Thomson Nov 22 '15 at 13:10
  • Sorry - just had another look at the javascript -I had an error in mine. Now it's returning a value (which looks to be the first value in the column) - but I need all values. – Michael Thomson Nov 22 '15 at 13:26
  • value[1] gets the second one? Maybe search or ask on stackoverflow.com. – Velkan Nov 22 '15 at 20:27
  • You're right! value(1) does indeed get the second value. I think I need to use an array to then capture all of the values? – Michael Thomson Nov 23 '15 at 17:26
  • It means that `value` variable is already an array. `value.join(', ')` should return a string with comma separated values. – Velkan Nov 23 '15 at 17:45
  • Apologies, Velkan, I'm being stupendously stupid today. I should have posted my code before. I've edited the original question with my current code - how do i enter the 'join' to this? – Michael Thomson Nov 23 '15 at 19:38

1 Answers1

0

Finally managed to get it to work :-). I'm posting my (working) code here in case anyone else hits a similar problem:

// Chart Data
function getData() {
var db = LocalStorage.openDatabaseSync("data.sql", "1.0",  "mileageDataBase", 10000000);
var ret;
var res = [];
var index;
db.transaction(
   function(tx) {
   var rs = tx.executeSql('SELECT * FROM mpg');
   for (var i=0; i<rs.rows.length; i++) {
   var row = rs.rows.item(i);
   ret = row.miles/row.gallons;
   res.push(ret);
  }
}
);
return res;
    }

Many thanks to Velkan for helping to point me in the right direction (even though the original question was more than a little ambiguous :-) )