[SOLVED] Import asset DB

Hello,

I have a SQL Lite DB which i need to use within my app.
My DB is located at myApp/assets/db/test.db

And i try to download my db into documentDirectory/SQLite.

But i have this error 'Unable to resolve module ‘./assets/db/test.db’

I used this example : https://github.com/expo/test-suite/blob/master/tests/SQLite.js.

And i tried this solution too : SQLite existing database - #5 by thoughtco

Here is my code :

import AppNavigator from "./navigation/Navigation";
import React from "react";
import { StyleSheet, View } from "react-native";
import { SQLite, FileSystem as FS, Asset } from "expo";

export default class App extends React.Component {
  componentDidMount() {
    FS.downloadAsync(
      Asset.fromModule(require("./assets/db/test.db")).uri,
      `${FS.documentDirectory}SQLite/test.db`
    );
  }
  render() {
    return (
      <View style={styles.container}>
        <AppNavigator />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff"
  }
});

And in my app.json :

  "packagerOpts": {
      "assetExts": ["db"]
    }

Can somebody explain to me what I’m doing wrong?

Thanks :slight_smile:

Hey.

I banged my head off this a few times too, but eventually I got around it by doing the following:

  1. created a function that made a dummy/blank DB and called it in component did mount:

     makeSQLiteDirAsync() {
     const dbTest = SQLite.openDatabase('dummy.db');
    
     try {
     	await dbTest.transaction(tx => tx.executeSql(''));
     } catch(e) {
     	if (this.state.debugEnabled) console.log('error while executing SQL in dummy DB');
     }
    

    }

this effectively creates the SQLite dir required.

  1. After this call I have a call similar to yours:

     // load in db
     Expo.FileSystem.downloadAsync(
     	Expo.Asset.fromModule(require('./assets/db/db.db')).uri,
     	`${Expo.FileSystem.documentDirectory}SQLite/db.db`
     )
     .then(function(){
    
     	let db = SQLite.openDatabase('db.db');
    
     	// do whatever else you need here
     
     })
    

Hope this helps!

2 Likes

also might be worth adding

"assetBundlePatterns": [ "**/*" ]

To app.json

Hello,

It works like a charm.

Thank you very much :slight_smile:

Great - glad I could save you some head scratching!

1 Like

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