Directory for database doesn't exist

Please provide the following:

  1. SDK Version: 37.0.0
  2. Platforms(Android/iOS/web/all): Android

Hi everyone, for the last couple of months I’ve been working on my first app project with react native and Expo.

I’m ready to finally launch my app but I’m having one big issue: The app uses a premade sqlite database to read and update information, this database gets loaded into the app the first time it launches or if the version has been updated (via a simple variable). I tested the app with no issues via the Expo Client but, now that I’m trying it in a phone (via an apk) there’s no db and I have no clue why it’s not working

Here’s the code that loads the db:

FileSystem.downloadAsync(
  Asset.fromModule(require('../databases/programs.db')).uri,
  `${FileSystem.documentDirectory}SQLite/programs-${version}.db`
).then(() => {
  programsDB = SQLite.openDatabase(`programs-${version}.db`);
  loadDB(loaded);
});

I have this in metro.config.js:

module.exports = {
  resolver: {
    blacklistRE: blacklist([/amplify\/#current-cloud-backend\/.*/]),
    assetExts: ["db", "ttf", "png", "jpg"]
  },
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
};

And this in app.json

"assetBundlePatterns": [
  "src/library/assets/**/*",
  "src/library/databases/*",
  "src/library/fonts/*"
],
"packagerOpts": {
  "assetExts": ["db"]
},

I’ve tried both with

require('../databases/programs.db'

and

require('library/databases/programs.db'

After the app tries to load stuff from the db I get the following error:
“Directory for /data/user/0/com.company.myApp/files/SQLite/programs-2020052401.db doesn’t exist”

I also tried changing the source db to download from .db to .mp4 after an answer I read elsewhere but it doesn’t do it either.

Any ideas? This is the last hurdle before I can finally launch my app.

SOLUTION

After tinkering with it to isolate the problem I ended up finding out that the issue was that the directory (SQLite) where the database is going to be saved in the devices wasn’t being created with downloadAsync, which I would have known if I had read the docs more carefully.
I just had to make the directory first if it didn’t exist and then it worked alright.
Here’s how the coded ended up looking:

// Check if the directory where we are going to put the database exists
let dirInfo;
try {
    dirInfo = await FileSystem.getInfoAsync(`${FileSystem.documentDirectory}SQLite`);
} catch(err) { Sentry.captureException(err) };
  
if (!dirInfo.exists) {
  try {
    await FileSystem.makeDirectoryAsync(`${FileSystem.documentDirectory}SQLite`, { intermediates: true });
  } catch(err) { Sentry.captureException(err) }
};

// Downloads the db from the original file
// The db gets loaded as read only
FileSystem.downloadAsync(
  Asset.fromModule(require('../databases/programs.db')).uri,
    `${FileSystem.documentDirectory}SQLite/programs${version}.db`
  ).then(() => {
  programsDB = SQLite.openDatabase(`programs${version}.db`); // We open our downloaded db
  loadDB(loaded); // We load the db into the persist store
}).catch(err => Sentry.captureException(err));

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.