Example of SQLite Database in React Native

Here is an Example of SQLite Database in React Native. We are hoping you have read our previous post on Local Database in React Native App. SQLite database is one of the databases that we have discussed in our previous post. To define the SQLite we can say it is an open source SQL database that stores data to a text file on a device. We can perform all the CRUD SQL transactions in this database and it is the most common database that we have heard of for mobile development. Let’s get started.

To use SQLite database in our Example we will use SQLite3 Native Plugin for React Native for both Android and iOS. It is based on Cordova SQLite plugin.

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

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

Please note down we are using callback function not real-time feedback because while running a query it does not provide the response instantly. Execution of query takes some time and give the response in the callback function.

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 Run the following commands to create a new React Native project This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use openDatabase we need to install react-native-sqlite-storage package. To install this

Open the terminal and jump into your project

Run the following command.

This command will copy all the dependencies into your node_module directory, You can find the directory in node_module the directory named react-native-sqlite-storage.

Linking of Library

To use SQLite library we have to make some changes in the following files:

Go to the SqliteExample> android > app > build.gradle.
Scroll down and add the following dependency.

Open SqliteExample> android > settings.gradle and add the following lines.

 

Open SqliteExample> android > app > src > main > java > com > sqliteexample> MainApplication.java  and add the following lines in imports.

Scroll down and add the following line in getPackages after new MainReactPackage(). Please add a comma(,) and then add

 

Here we have done with our installation of the library. If you are still facing the trouble please comment below so that we can help you out from the problem.

Database Setup

While working with the React Native it provides two option either you can use the blank DB file to start with and make your database tables at runtime or you can use the pre-populated database so that you can start working directly.

We are making a blank file with the extension .db and placing it in www folder and going to make the database tables at runtime which is a very good practice.

 

In this Example, we are going to use createStackNavigator component to navigate so we need to install the react-navigation library. If you are new to the React Native Navigation then this post will help you.

To install react-navigation run the following command on terminal

Just to confirm the dependencies here is the screenshot of the dependencies that we are using ;

If you are on the same boat then let’s go further or please update your package.json and run npm install to install new dependencies.

For this Example, we are using the following structure. You have to make page directory in SqliteExample and components directory under that. Make the marked js files too.

You can download the code from Github and can run directly

OR

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. To run the project on an Android Virtual Device or on real debugging device or on the iOS Simulator by running

               
               
               

This is how you can use SQLite Database in React Native Application. 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. 🙂

How useful was this post?

Click on a star to rate us!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

About Snehal Agrawal

#SelfLearner #MobileAppDeveloper #TechnologyMiner #OpenSourceLover thats enough to describe me..

View all posts by Snehal Agrawal →

14 Comments on “Example of SQLite Database in React Native”

  1. I am doing POC on Sqlite with React Native. when i try execute below line getting complie time error. import org.pgsqlite.SQLitePluginPackage, it is expecting from value

    1. Can you please check the SQLite version you are using?
      We have used the following version for the example. If you are using any other version then please let me know so that we can run the trial with that or share the screenshot of the log to help you out.
      “react-native-sqlite-storage”: “^3.3.6”,

    2. Hello Naresh,
      Finally, I faced the same problem now and find a solution. Please open the following file

      SqliteProject/node_modules/react-native-sqlite-storage/src/android/build.gradle

      and replace
      compile ‘com.facebook.react:react-native:0.14.+’ (Or other version)
      with
      compile ‘com.facebook.react:react-native:+’

      This will solve your problem.

  2. Hello,
    why using setstate in constructor in VIEWAllUSERS.js ?
    because in react lifecycle we should not use setstate in constructor

    1. Setting up state in constructor means you are assigning some default value to the state variable. If you do not want to setState in constructor you can skip it but if you try to get the value without setting the value then it will give you a state undefined error so you have to remember that first set the state and then get the value from state.

  3. I am using SQLite version 3.3.9. I am trying to run on the ios, i get the following error on the screen –

    null is not an object (evaluating ‘NativeModules[“SQLite”][method]’)

    exec
    sqlite.core.js:88:26
    open
    sqlite.core.js:279:16
    SQLitePlugin
    sqlite.core.js:110:12

    sqlite.core.js:778:26

    sqlite.core.js:77:22

    HomeScreen.js:30:11
    loadModuleImplementation
    require.js:321:6

    App.js:13
    loadModuleImplementation
    require.js:321:6

    index.js:7
    loadModuleImplementation
    require.js:321:6
    guardedLoadModule
    require.js:199:45
    global code
    :0

    1. Hello Tara,
      Finally, I faced the same problem now and find a solution. Please open following file

      SqliteProject/node_modules/react-native-sqlite-storage/src/android/build.gradle

      and replace
      compile ‘com.facebook.react:react-native:0.14.+’
      with
      compile ‘com.facebook.react:react-native:+’

      This will solve your problem.

    1. Are you talking about React?
      Web platform does not support SQL database. You can use the browser database to store the data in your browser. Can you please confirm which web technology you want to use?

Leave a Reply

Your email address will not be published. Required fields are marked *

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