andpor / react-native-sqlite-storage

Full featured SQLite3 Native Plugin for React Native (Android and iOS)
MIT License
2.75k stars 521 forks source link

Re-Open newly created Database #508

Closed Stophface closed 2 years ago

Stophface commented 2 years ago

Expected Behavior

I want to re-open a closed database without loosing its content.

Current Behavior

I think this is what is happening: Upon reopening, it is creating a new database with the same name of the database I want to re-open, overwriting my original database.

Steps to reproduce

I am creating a database foo with the table bar like this:

import SQLite from 'react-native-sqlite-storage';

SQLite.enablePromise(true);

const database = SQLite.openDatabase(
  {
    name: 'foo.db',     
    location: 'Library'
  },
  console.log('sucessfully created database'),
  error => {
    console.log('failed to  create database', error)
  }
);

database.transaction(tx => {
      tx.executeSql('CREATE TABLE IF NOT EXISTS bar(....);'
});

I then close this database

database.close();

Later in my App, I want to reopen the database and perform an INSERT. I use the same code I used for creating the database

const database = SQLite.openDatabase(
  {
    name: 'foo.db',     
    location: 'Library'
  },
  console.log('sucessfully created database'),
  error => {
    console.log('failed to  create database', error)
  }
);

And then I perform the INSERT

database.transaction(tx => {
            tx.executeSql('INSERT INTO bar ...')
});

This gives me

Possible Unhandled Promise Rejection (id: 0):
Object {
  "code": 5,
  "message": "near \"NOLL\": syntax error",
}

and

Possible Unhandled Promise Rejection (id: 1):
Object {
  "code": 5,
  "message": "incomplete input",
}

and

Possible Unhandled Promise Rejection (id: 2):
Object {
  "code": 5,
  "message": "no such table: bar",
}

I assume it's recreating the database in my second openDatabase() statement, overwriting everything that was there - including the table bar.

Context

I want to be able to re-open a database that has been created by this library.

Your Environment

Stophface commented 2 years ago

Ah well... I had a typo upon creating the table. Thats why the table was not created at all.