Example to Load Pre Populated SQLite Database in React Native

Here is an Example to Load Pre Populated SQLite Database in React Native. In this example, we will use SQLite Browser to create an SQLite Database and then we will use this database.

For SQLite database, we will use the SQLite3 Native Plugin which will work in both Android and iOS. It is based on Cordova SQLite plugin.

This is an extended post of our previous post on Example of SQLite Database in React Native so if you stuck with anything then you can visit the last post because this post will have more details about the importing of the pre-populated database.

In this Example, we will make a HomeScreen with the option to go to other screens like

  • RegisterUser: To Register the User. (Create/Insert)
  • ViewAllUser: To View All Users. (Read)
  • ViewUser: To View Singel Users By Id. (Read)
  • UpdateUser: To Update the User.(Update)
  • DeleteUser: To Delete the User.

We will be having some custom components like Mybutton, Mytext, Mytextinput which will be used in place of react-native Button, Text, and TextInput.

How to Use

You just have to import the library like this:

and open the database using (Here my pre-populated database name is user_db.db )

Now, whenever you need to make some database call you can use db variable to execute the database query.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

This example is updated for the React Navigation 4.0+. For more about React Navigation 4 please have a look at Using React Navigation 4.0 in React Native apps.

To install the dependencies open the terminal and jump into your project

1. Install react-native-sqlite-storage dependency to use SQLite

2. Install react-navigation dependency to import createAppContainer

3. Install react-native-gesture-handler dependency to support gesture navigation

4. Install react-navigation-stack to import createStackNavigator

These commands will copy all the dependencies into your node_module directory.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods.

In this example we need to install the pods for react-native-gesture-handler and react-native-sqlite-storage.

Please use the following command to install CocoaPods

Database Setup for Pre-Populated Database

1. First of all, download the SQLite Browser from here and open the browser

2. Create a new database

3. Save with any name. In my case it is user_db.db

4. Now after creating a database please create a table by clicking on Create Table

5. It will open a dialog where you have to enter the table name and have to add the fields then click ok

6. The click on the Browse Data tab and select your table i.e. tbl_user. Now add a new record in this table

7. For this example, I have added 2 records. You can see by selecting the Table. Please save the database after that because all this activity are not saved automatically. You have to find the save option and have to save this.

It is my request to close the browser once and open the database again to be sure it is properly saved.

8. This is how you can make the SQLite database in SQLite browser. Please make a directory named www and place the database into it. This will be the directory which we will copy in Android and IOS.

Now we have to copy this database in different places for Android and IOS because default places to put the database is different for Android and IOS. Please be sure to put the database on the below-mentioned places.

Placing of Pre-populated Database in Android

1. In case of Android copy the www directory into android > app > src > main > assets

2. You can also copy using Android Studio

Placing of Pre-populated Database in IOS

1. Open your xcworkspace in Xcode


2. After opening the project right click on the Project folder (SQLiteExample in this case) and select Add Files

3. Navigate to www directory and select www directory with create folder references and SQLiteExample. Then click Add

4. You can see the added directory in the project structure

Here we have done all the configuration for the pre-populated database. Please follow the below instruction for the example.

Project Structure

We will perform a complete CRUD operation in this example. Please create the following project structure and copy the code given below

Open the project directory and replace the following code

App.js

Mybutton.js

Mytext.js

Mytextinput.js

HomeScreen.js

RegisterUser.js

UpdateUser.js

ViewAllUser.js

ViewUser.js

DeleteUser.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Here are the screenshots of the final outcome

         
         

This was the Example to Load Pre Populated SQLite Database in React Native. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

21 thoughts on “Example to Load Pre Populated SQLite Database in React Native”

  1. OPEN database: DST.db
    K:\ReactEx\SQLiteEx\node_modules\react-native\Libraries\Utilities\infoLog.js:16 Running application “SQLiteEx” with appParams: {“rootTag”:1}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
    K:\ReactEx\SQLiteEx\node_modules\react-native-sqlite-storage\lib\sqlite.core.js:131 new transaction is waiting for open operation

    In HomeScreen.js table does not created

  2. Hi
    Thank you so much for this awesome tutorial
    But can you please guide me in uploading multiple images in sqlite database and get back those images in app using react native

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.