首页 > 解决方案 > 初始化 SQLite 数据库

问题描述

我在以下代码中使用“react-native-sqlite-storage”

import SQLite from 'react-native-sqlite-storage';
SQLite.DEBUG(true);
SQLite.enablePromise(true);
//"test.db", "1.0", "Test Database", 200000, openCB, errorCB)
const db = SQLite.openDatabase({name: 'mydb.db'});
//'CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY NOT NULL, date TEXT NOT NULL, start TEXT NOT NULL, end  TEXT NOT NULL);'
export const initdb = () => {
  const promise = new Promise((resolve, reject) => {
    db.transaction(tx => {
      
      tx.executeSql(
        'CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY NOT NULL, date TEXT NOT NULL, start TEXT NOT NULL, end  TEXT NOT NULL);',

        [],
        () => {
          resolve();
        },
        (_, err) => {
          reject(err);
        },
      );
    });
  });
  return promise;
};

我正在使用以下代码

initdb()
  .then(() => {
    console.log('Sucessfully Initialised Databse ');
  })
  .catch(error => {
    console.log('Failed Database');
    console.log(error);
  });

我怎么在日志中得到这个错误

LOG  Promise based runtime ready
 LOG  OPEN database: mydb.db
 LOG  SQLite.open({"name":"mydb.db","dblocation":"nosync"})
 LOG  Failed Database
 LOG  [TypeError: undefined is not a function (near '...db.transaction...')]
 LOG  Running "timekeep" with {"rootTag":1,"initialProps":{}}

我在ios模拟器上试试

标签: react-nativereact-native-sqlite-storage

解决方案


推荐阅读