Trouble loading existing database.

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?

I’ve been working on this for 1 week as well, trying both ‘expo-sqlite’ and ‘react-native-sqlite-storage’ and I finally made it work on my dictionary app by editing the code on this expo forum: Cannot read from Bundled Database

That openDatabase code on Expo didn’t work on my app either.

Prior to this, I put the SQL data into ./asset/db/

import * as SQLite from 'expo-sqlite';
import * as FileSystem from 'expo-file-system';
import {Asset} from 'expo-asset';
import { openDatabase } from 'expo-sqlite';

export class DictionaryScreen extends React.Component{
    state = {inputValue: "Look up the word"};

    _handleTextChange = inputValue => {
        this.setState({inputValue});
    };

    async openDatabase() {
        if (!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'SQLite')).exists) {
          await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'SQLite');
        }
        await FileSystem.downloadAsync(
          Asset.fromModule(require('/Users/***/***/assets/db/test.db')).uri,
          FileSystem.documentDirectory + 'SQLite/test.db'
        );
        return SQLite.openDatabase('test.db');
    }

    db = openDatabase('test.db');

    searchWord() {
        this.db.transaction((tx) => {
        tx.executeSql(
            'SELECT word, mean FROM items WHERE word LIKE "%?%";',
            [this.state.inputValue],
            () => {
                console.log("success");
            },
            () => {
                console.log("failed");
            }
            
         );
        });
    };
    
    render() {
        return(
            <SafeAreaView style={styles.container}>
                <View style={styles.searchContainer}>
                    <MaterialCommunityIcons name="text-search" size={20}/>
                    <TextInput
                        style={styles.input}
                        onChangeText={this._handleTextChange}
                        onSubmitEditing={(inputValue) => this.searchWord(inputValue)}
                        value={this.inputValue}
                        placeholder='Look up the word'
                        keyboardType="default"
                    />
                <StatusBar style='light' />
                </View>
            </SafeAreaView>
        );
        }
    }

Make sure all props are defined.

Hope this will help you.

Hi,
I am getting an error, None of these file exists