dimanche 26 avril 2015

Using Sqlite in Qt (Qml File)

I have the weirdest error ever using sqlite, I'm trying to connect to a database, create a table, insert some values & reading them later on, Simple enough isn't it, but the weird thing is that i'm getting an error in one part of my application unlike the other part which is basically identical to the that part, for demonstration here is my first part which is working just fine:

Rectangle{
        id:enterYourName

function getDatabase() {
     return LocalStorage.openDatabaseSync("HomeScreen", "1.0", "StorageDatabase", 100000);
}


function initialize() {
    var db = getDatabase();
    db.transaction(
        function(tx) {                                                         

            tx.executeSql("CREATE TABLE IF NOT EXISTS settings (setting TEXT UNIQUE, value TEXT)");
      });
}


function setSetting() {
   var db = getDatabase();
   db.transaction(function(tx) {

                   var rs = tx.executeSql("INSERT OR REPLACE INTO settings VALUES (?,?);", ['myName',mytextInput.text]);

        }
  );
}




function getSetting() {
   var db = getDatabase();
   var res="";
   db.transaction(function(tx) {
       var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', ['myName']);

     if (rs.rows.length > 0) {
          res = rs.rows.item(0).value;
     } else {
         res = "Unknown";
     }
  })

  return res;
}




 TextInput{
            id:mytextInput
            anchors.centerIn: parent
            text:enterYourName.getSetting()
            maximumLength: 12
            font.family: harlow_Font.name
            font.pixelSize: 0.029*windowid.width
            visible: true
        }

}

& here is the second part, In the second part im doing exactly the same thing as above but i'm getting an error saying: Unable to assign [undefined] to QString when the line enterYourName2.getRedValue() located in TextInput is executed

Rectangle{
        id:enterYourName2

 function getDatabase() {
             return LocalStorage.openDatabaseSync("SecondHome", "1.0", "StorageDatabase", 100000);
        }


        function initialize() {
            var db = enterYourName2.getDatabase();
            db.transaction(
                function(tx) {
                    tx.executeSql("CREATE TABLE IF NOT EXISTS mytable (redp TEXT UNIQUE, redvalue TEXT)");

              });
        }



        function setRedValue() {
           var db = enterYourName2.getDatabase();
           db.transaction(function(tx) {            
                           var rs = tx.executeSql("INSERT OR REPLACE INTO mytable VALUES (?,?);", ['myName2',mytextInput2.text]);

                }
          );
        }



        function getRedValue() {
           var db = enterYourName2.getDatabase();
           var res="";
           db.transaction(function(tx) {
               var rs = tx.executeSql('SELECT redvalue FROM mytable WHERE redp=?;', ['myName2']);

             if (rs.rows.length > 0) {
                  res = rs.rows.item(0).value;
             } else {
                 res = "Unknown";
             }
          })

          return res;
        }



        Component.onCompleted: enterYourName2.initialize()




 TextInput{
            id:mytextInput2
            anchors.centerIn: parent
            text:enterYourName2.getRedValue()  //The Error is raised over here
            maximumLength: 12
            font.family: harlow_Font.name
            font.pixelSize: 0.029*windowid.width
            visible: true
        }

}

Any ideas on why this error appeared & how it can be resolved?

Aucun commentaire:

Enregistrer un commentaire