Really at my wits end here trying to load an existing database into expo SQLite. The example project included w/ the expo bundles doesn’t show an example of SQLite being loaded from a pre-existing DB, nor does the boilerplate code shown on the expo-sqlite docs work as given.
Currently, I’m trying to rework the example project to load from a pre-existing DB rather than one generated the first time the app loads. So aside from calling the snippet below from within a useEffect function my code is exactly the same as the example.
async function openDatabase(pathToDatabaseFile: string): SQLite.WebSQLDatabase {
if (!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'SQLite')).exists) {
await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'SQLite');
}
await FileSystem.downloadAsync(
Asset.fromModule(require(pathToDatabaseFile)).uri,
FileSystem.documentDirectory + 'SQLite/myDatabaseName.db'
);
return SQLite.openDatabase('myDatabaseName.db');
}
Simply copying the snippet doesn’t seem to be working for me and the particular sequence “(pathToDatabaseFile: string): SQLite.WebSQLDatabase” seems to be an issue. Left as is my code will reply “App.js: App.js:Invalid call at line 15: require(pathToDatabaseFile)”
If I were to change the “pathToDatabaseFile” to refer to a string that actually was the static path of the db file I am then given the same error.
I’ve found all kinds of similar links to functions but never the functions implementation en-situ. For example one, to be called in componentDidMount that seemed promising:
async openExistingDatabase() {
const internalDbName = "dbInStorage.db"
const sqlDir = FileSystem.documentDirectory + "SQLite/";
if (!(await FileSystem.getInfoAsync(sqlDir + internalDbName)).exists) {
await FileSystem.makeDirectoryAsync(sqlDir, {intermediates: true});
const asset = Asset.fromModule(require("../assets/db/sqlite3.db"));
await FileSystem.downloadAsync(asset.uri, sqlDir + internalDbName);
}
this.database = SQLite.openDatabase(internalDbName);
}
However this gives the common error:
None of these files exist:
* sqlite3.db
* ../assets/db/sqlite3.db/index(.native|.ios.expo.ts|.native.expo.ts|.expo.ts|.ios.expo.tsx|.native.expo.tsx|.expo.tsx|.ios.expo.js|.native.expo.js|.expo.js|.ios.expo.jsx|.native.expo.jsx|.expo.jsx|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json)
And yes I already made the changes to my metro.config.js.
Most of the topics asking the same question are just left without answer, which is super ironic because the expo-sqlite documentation has NO full working examples of a preloaded SQLite database. Which strikes me as incredibly ironic because from my experience with SQL databases, prepopulation is the exception rather than the rule. It’s a bit of a glaring omission. Am I to assume that creating AND seeding the DB the first time an app launches, rather than having it already existing is best practice when using expo?