【问题标题】:Why variable len value not changing in db transaction?为什么变量 len 值在 db 事务中没有改变?
【发布时间】:2020-11-25 05:30:49
【问题描述】:
    //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;

}

【问题讨论】:

    标签: javascript android ios react-native android-sqlite


    【解决方案1】:

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

    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)
            }
          });
        })
      }
    

    【讨论】:

    • 我只想返回表格的长度
    • 对象作为反应子项无效(找到带有键 _u _v _w _x 的对象)
    • 你能修改我的getFavoritesLength()方法吗
    猜你喜欢
    • 2022-12-06
    • 1970-01-01
    • 2015-09-30
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多