react-native-sqlite-storage icon indicating copy to clipboard operation
react-native-sqlite-storage copied to clipboard

Can't get attach to work

Open nastarfan opened this issue 6 years ago • 5 comments

I tried to make query that involves tables from two different databases, however i cannot get sqlite attach to work properly even though debug message says the database is attached successfully.

Expected Behavior

Using attach I can make query involving multiple databases

Current Behavior

Cannot select table from the attached database

Possible Solution

Steps to Reproduce (for bugs)

    let queryResults = [];
    await SQLite.openDatabase(
      { name: 'second.db', createFromLocation: 1 },
      () => console.log('success open second database'),
      () => console.log('failed to open second database'),
    );
    const dbMaster = await SQLite.openDatabase(
      { name: 'master.db', createFromLocation: 1 },
      () => console.log('success open master database'),
      () => console.log('failed to open master database'),
    );
    dbMaster.attach(
      'second',
      'second',
      () => console.log('second is attached successfully'),
      () => console.log('error attaching second'),
    );
    dbMaster.readTransaction((tx) => {
        tx.executeSql(
          `select * from second.translation where id = ${id}`,
          [],
          (tx, results) => {
            const rows = results.rows.raw();
            rows.forEach((row) => {
              queryResults.push(row.text);
            });
          },
        );
      }),

Context

I got the following error suggesting that the attached table cannot be read:

Object {
  "code": 0,
  "message": "no such table: second.translation (code 1 SQLITE_ERROR): , while compiling: select * from second.translation where id = 1",
}

If my query only involves master database, it works fine but everytime I access table from the attached database (second database), I get that error. I tried to follow code given in example directories of this library but still the same result

Your Environment

  • React Native SQLite Storage Version used: "^3.3.10"
  • React Native version used: "0.59.5"
  • Operating System and version (simulator or device): android device & simulator

Debug logs

Database master is successfully opened
D:\rn\masterApp\node_modules\react-native-sqlite-storage\lib\sqlite.core.js:328 ATTACH database second to master.db with alias second
D:\rn\masterApp\node_modules\react-native-sqlite-storage\lib\sqlite.core.js:86 SQLite.attach({"path":"master.db","dbName":"second","dbAlias":"second"})
D:\rn\masterApp\node_modules\react-native-sqlite-storage\lib\sqlite.core.js:131 new transaction is waiting for open operation
D:\rn\masterApp\node_modules\react-native-sqlite-storage\lib\sqlite.core.js:86 SQLite.backgroundExecuteSqlBatch({"dbargs":{"dbname":"master.db"},"executes":[{"qid":1111,"sql":"SELECT 1","params":[]},{"qid":1111,"sql":"select * from second.translation where id = 1","params":[]}]})
D:\rn\masterApp\src\utils\Database.js:65 Database is attached successfully
D:\rn\masterApp\node_modules\react-native-sqlite-storage\lib\sqlite.core.js:576 warning - exception while invoking a callback: {"code":0}
D:\rn\masterApp\node_modules\react-native-sqlite-storage\sqlite.js:66 error:  readTransaction ƒ (tx) {
            tx.executeSql("select * from second.translation where id = " + id, [], function (tx, results) {
              var rows = results.rows.raw();
              rows.forEach(… Arguments [{…}, callee: ƒ, Symbol(Symbol.iterator): ƒ]
D:\rn\masterApp\node_modules\react-native\Libraries\YellowBox\YellowBox.js:67 Possible Unhandled Promise Rejection (id: 0):
Object {
  "code": 0,
  "message": "no such table: second.translation (code 1 SQLITE_ERROR): , while compiling: select * from second.translation where id = 1",
}

nastarfan avatar Jul 14 '19 15:07 nastarfan

any help for this issue? @andpor

nastarfan avatar Jul 21 '19 01:07 nastarfan

Hi @nastarfan, I know it has been 6 months but I'm facing the same issue; I've been trying to attach a simple database for more than 16h; I can do it from the terminal inside the adb shell command, but using the library returns the same error as you showed. Any solution on this?

betoharres avatar Jan 08 '20 19:01 betoharres

I have some news on this. I was only testing on android, but apparently iOS works just fine, and on android the attach method is not working properly for some reason.

Needs more investigations. I'll post it here if I discover something.

betoharres avatar Jan 09 '20 14:01 betoharres

I got it working, the library works just fine, the problem is that we need more info into the documentation. (btw, the problem was on Android, iOS works with no problem, ~~but this fix will work on both platforms~~ it will work only on Android, I ended up needing to create an if/else of the Platforms, and on iOS I just passed the database's name only)

I discovered the problem by looking at this line right here: https://github.com/andpor/react-native-sqlite-storage/blob/master/platforms/android/src/main/java/org/pgsqlite/SQLitePlugin.java#L509 The first argument of attach needs to be the FULL PATH, including the filename with the extension.

case background: My case was that the app downloads a pre populated database, and I'm using this library to connect to the varios databases that were being downloaded so I can attach to them forming one big database.

edit: the second argument is just the alias;

React 16.9 RN: 0.61.1

betoharres avatar Jan 12 '20 02:01 betoharres

Hi @betoharres sorry, I didn't get it working last time and decided to make separate query for separate database instead of using attach. I'm glad you managed to make it work and thanks for explaining it. Will try to incorporate your solution later.

I will leave this issue open so that it gets noticed about the need to modify the docs. Maybe you can modify it and make a pull request @betoharres since I haven't tested it myself

nastarfan avatar Jan 28 '20 04:01 nastarfan