簡體   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