繁体   English   中英

为什么变量 len 值在 db 事务中没有改变?

[英]Why variable len value not changing in db transaction?

    //this is constructor of my code
    constructor(props) {
        super(props);
        this.state = {
        textValue:this.getFavoritesLength(),
        };
    }

//这个sqlite代码db.transaction执行prefect但它不改变var len的值它总是返回0,而我改变len的值作为表长度。

getFavoritesLength()
{
    var len = 0;
    db.transaction(tx => {
        tx.executeSql(
            'SELECT * FROM table_favorites', [],
            (tx, results) => {
                len = results.rows.length;
                console.log('select * results are = ', len);
                // if (len > 0) {
                //   return len;
                // }else{
                //   return 0;
                // }
            }
        );
    });
    return len;

}

在运行查询时,它不会立即提供响应。 查询的执行需要一些时间并在回调函数中给出响应。 你可以尝试使用异步吗

insertarDatos = () => {
    return new Promise((resolve, reject) => {
      db.transaction((tx) => {
        try {
          /* Successful transaction */
          /* Make sure to call resolve when the transaction has finished, maybe 
             db.transaction has a success callback
          */
          resolve()
        } catch (error) {
          /* Failed transaction */
          // if you reject any eventual errors, you can catch them when calling the function insertarDatos

          reject(error)
        }
      });
    })
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM