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

Could not open database

Open GuilhermeAB opened this issue 5 years ago • 6 comments

have a problem opening the sqlite database. I have a request that downloads a .zip and then unzip it to open the sqlite database.

  // imports
  import SQLite from 'react-native-sqlite-storage';
  import { downloadFile, CachesDirectoryPath } from 'react-native-fs';
  import { unzip } from 'react-native-zip-archive';
// Download File 'database.zip'
  function downloadDatabase () {
    const path = `${CachesDirectoryPath}/database.zip`;

    downloadFile({
      fromUrl: 'http://172.17.146.225:3001/database',
      toFile: path,
    }).promise.then((response) => {
      if (response.statusCode === 200) {
        // Unzip the file
        unzipDatabaseFile(`${path}`);
      } else {
        console.log('SERVER ERROR');
      }
    }).catch((err) => {
      if (err.description === 'cancelled') {
        console.log('cancelled by user');
      }
      console.log(err);
    });
  }

  function unzipDatabaseFile (filePath: string) {
    unzip(filePath, CachesDirectoryPath).then(
      async (path) => {
        console.log(`unzip completed at ${path}`);
        openDatabase(path);
      },
    ).catch((error) => {
      console.log('zip', error);
    });
  }

  // Open the database
  function openDatabase (path: string) {
    try {
      SQLite.openDatabase({ name: 'xdatabase', createFromLocation: `${path}/xdatabase.sqlite`, location: 'default' }, () => {
        console.log('Success');
      }, (e) => {
        console.log('OpenDatabase Error', e);
      });
    } catch (error) {
      console.log('catch =>', error);
    }
}

but I always get the following message:

 LOG  FILE DOWNLOAD!
 LOG  unzip completed at /data/user/0/com.nonameapp/cache
 LOG  OpenDatabase Error [Error: Could not open database]

Can anyone help? I always get that same error. I don't know what else to try, I've tried to change the 'createFromLocation' in every possible way.

GuilhermeAB avatar Feb 07 '20 01:02 GuilhermeAB

Having the same issue. I'm not sure but it's seems like the path provided to createFromLocation is relative. Have you fixed it ?

smontlouis avatar Feb 10 '20 09:02 smontlouis

Not yet :(

GuilhermeAB avatar Feb 10 '20 14:02 GuilhermeAB

I managed to fix my issue

I used Android Device File Explorer to inspect my device files, and noticed that the path created was a folder containing the file, so I was actually trying to open a folder as a database. Try to use Android Device File Explorer to understand what is your issue.

smontlouis avatar Feb 10 '20 20:02 smontlouis

Sry, but that was I tried:

My path: ${path}/xdatabase.sqlite

openDatabase({ name: 'xdatabase', createFromLocation: `${path}/xdatabase.sqlite`, location: 'default' }

My android device file explorer: image

Any idea?

GuilhermeAB avatar Feb 10 '20 20:02 GuilhermeAB

This is what I've done

const sqliteDirPath = ${RNFS.DocumentDirectoryPath}/SQLite
await unzip(`${RNFS.MainBundlePath}/www/strong.sqlite.zip`, sqliteDirPath)

and then

this.dbStrong = SQLite.openDatabase(
      {
        name: 'strong.sqlite',
        readOnly: true,
        createFromLocation: '/SQLite/strong.sqlite'
      },
      () => {},
      e => console.log(e)
    )

smontlouis avatar Feb 10 '20 20:02 smontlouis

The error persist

  import { DocumentDirectoryPath} from 'react-native-fs';

  unzip(filePath, `${DocumentDirectoryPath}/databases`).then(
      () => {
        openDatabase();
      },
    ).catch((error) => {
      console.log('zip', error);
    });

and openDatabase:

  try {
      SQLite.DEBUG(true);
      // Already tried this, and others paths:   '/data/databases/xdatabase.sqlite'
      const db = SQLite.openDatabase({ name: 'xdatabase.sqlite', createFromLocation: `${DocumentDirectoryPath}/databases/xdatabase.sqlite`, location: 'Documents' }, () => {
        console.log('success');
      },
      (e) => {
        console.log(e);
      });
    } catch (error) {
      console.log('catch =>', error);
    }

With SQLite.DEBUG(true); I get:

 LOG  OPEN database: xdatabase.sqlite
 LOG  SQLite.open({"name":"xdatabase.sqlite","createFromLocation":"/data/user/0/com.nonameapp/files/databases/xdatabase.sqlite","location":"Documents","dblocation":"docs","assetFilename":"/data/user/0/com.nonameapp/files/databases/xdatabase.sqlite"})
 LOG  new transaction is waiting for open operation
 LOG  OPEN database: xdatabase.sqlite failed, aborting any pending transactions
 LOG  [Error: Could not open database]

I tried in Android Emulator and physical device.

Android Device File explorer (emulator): image

GuilhermeAB avatar Feb 14 '20 05:02 GuilhermeAB