首页 > 解决方案 > 反应原生 react-native-sqlite-storage 不工作

问题描述

我在我的项目中使用 react native react-native-sqlite-storage 并创建了 2 个表,但是在选择查询中,此代码不打印任何内容。

import {openDatabase} from "react-native-sqlite-storage";
.
.
.

export function selectAllData() {
    const db = openDatabase({name: 'myDb.db'});
    db.transaction(function (txn) {
        txn.executeSql('SELECT * FROM TABLE_NAME', [], (tx, results) => {
            let len = results.rows.length;
            console.log('len', len);
            for(let i = 0; i < len; i++)
                console.log(results.rows.item(i));
        });
    })
}

我希望这段代码打印表格的所有行,但没有打印。

标签: databasesqlitereact-native

解决方案


尝试这个

db.transaction(tx => {
  tx.executeSql('SELECT * FROM TABLE_NAME', [], (tx, results) => {
    var temp = [];
    for (let i = 0; i < results.rows.length; ++i) {
      temp.push(results.rows.item(i));
    }
    console.log('TABLE_NAME', temp)

  });
});

推荐阅读