无法在本机反应中将数据插入 SQLite 数据库

问题描述

我一直在 React Native 中使用 sqlite 数据库,但我不知道实际上有什么问题。

我正在打开数据库,创建表并尝试插入一些随机数据,但是当我尝试显示插入的数据时,有空数组。 我不确定是插入或显示错误,还是我的整个数据库打开错误

在这里打开数据库

import sqlite from 'react-native-sqlite-storage';
sqlite.DEBUG(false);
sqlite.enablePromise(false);

export const db = sqlite.openDatabase(
    {
        name: 'baza.db'
    }
);

在这里我正在创建表格并尝试插入数据并显示它:

fun = () => {
      db.transaction(function (txn) {
        txn.executesql(
          "SELECT name FROM sqlite_master WHERE type='table' AND name='Test_Table'",[],function (tx,res) {
            console.log('item:',res.rows.length);
            if (res.rows.length == 0) {
              txn.executesql('DROP TABLE IF EXISTS Test_Table',[]);
              txn.executesql(
                'CREATE TABLE IF NOT EXISTS Test_Table(test_id INTEGER PRIMARY KEY AUTOINCREMENT,test_value FLOAT,test_date VARCHAR(15))',[]
              );
            }
          }
        );
      })
    }

    
    insertData = () => {
      db.transaction(function (tx) {
        tx.executesql(
          'INSERT INTO Test_Table (test_value,test_date) VALUES (2,test)',(tx,results) => {
            console.log('Results',results.rowsAffected);
            if (results.rowsAffected > 0) {
              console.log('Data Inserted Successfully....');
            } else console.log('Failed....');
          }
        );
      });
      this.viewTable();
    }

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

我在创建表和插入数据后的日志:

[Info] 06-10 10:47:50.486  5866  5924 I ReactNativeJS: 'item:',0


[Info] 06-10 10:47:55.412  5866  5924 I ReactNativeJS: []

知道出了什么问题,或者我应该怎么做?我将不胜感激

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)