Example of Creating SQLite Database Table in React Native

SQLite is a RDBMS which full form is Relational Database Management System. To store data locally in mobile application SQLite Database is used. In today’s tutorial we would learn about Creating SQLite Database and Table in React Native. We would use React Native’s NPM react-native-sqlite-storage plugin to perform all the various task on SQLite. In this tutorial we would highlight 3 basic concepts of SQLite.

Contents in this project Example of Creating SQLite Database Table in React Native:

1. The first step is to download and install react-native-sqlite-storage NPM plugin in your current react native project. So open your react native project Root folder in Command Prompt in Windows and Terminal in MAC OS and execute below command.

Screenshot:

Screenshot after done installation:

2. In React Native version bigger than 0.60 there is no need to execute any additional command in Android, However for iOS we have to execute pod installation command to link the newly installed SQLite Storage Plugin to project. So execute below command in MAC OS for iOS react native project.

Screenshot:

Now our both Android and iOS project is ready to use with SQLite Storage.

3. Open your project’s main App.js file and import SafeAreaView, Text, View, StyleSheet, Alert and TouchableOpacity component.

4. Import openDatabase from ‘react-native-sqlite-storage‘ plugin.

5. Creating a new database named as SchoolDatabase.db and assign the database control to variable db.

6. Creating our main export default functional component named as App. This is our main View component.

7. Creating a function named as createTable(). Inside the method we would make a table named as Student_Table with fields student_id, student_name , student_phone and student_address. In this method first we would check whether there are already a table named as Student_Table is present or not if not present then it will create a new table and if present then it’ll not create a new table.

8. Creating return block. In the return block we would make Safe Area View component -> View component and make a Touchable Opacity component. We would call the createTable() method on button onPress event.

9. Creating Style.

10. Complete Source Code for App.js file:

Screenshots:

Example of Creating SQLite Database Table in React Native

Leave a Reply

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