反应本机react-native-sqlite-storage不起作用

时间:2019-08-25 10:15:32

标签: database sqlite react-native

我在项目中使用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));
        });
    })
}

我希望这段代码可以打印表格的所有行,但是什么也不会打印。

1 个答案:

答案 0 :(得分:-1)

尝试

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)

  });
});