在 Qt 中使用 Sqlite(Qml 文件)

Using Sqlite in Qt (Qml File)

我在使用 sqlite 时遇到了最奇怪的错误,我正在尝试连接到数据库,创建一个 table,插入一些值并稍后读取它们,这很简单,不是吗,但是奇怪的是,我在我的应用程序的一部分中遇到错误,而另一部分基本上与该部分相同,这里的演示是我的第一部分,它工作得很好:

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
        }

}

& 这是第二部分,在第二部分中,我做的事情与上面完全相同,但我收到一条错误消息:Unable to assign [undefined] to QString 当位于 TextInput 中的行 enterYourName2.getRedValue() 是执行

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
        }

}

关于出现此错误的原因以及如何解决它有什么想法吗?

res = rs.rows.item(0).value 应该是 res = rs.rows.item(0).redvalue;