View All Data From SQLite Database in React Native FlatList Example

In our previous tutorial’s we had learn about SQLite database and perform data insertion using Text Input component into SQLite. Today we would learn about View All Data From SQLite Database in React Native into FlatList component. If you read our previous tutorial then you’ll know back there we would use only single screen but today we would require 2 screens for our application. First screen for inserting data and second screen for showing all SQLite data into FlatList component. So we are using React Navigation library here to create multiple activity screen structure. It is necessary to install both NavigationContainer and createStackNavigator components in current project in order to use multiple screens.

React Native plugins we’re using in this tutorial:

  1. React Navigation – NavigationContainer and createStackNavigator to use multiple screens.
  2. react-native-sqlite-storage – To use SQLite database in react native Android iOS platforms.

Contents in this project View All Data From SQLite Database in React Native FlatList Example:

1. Open our React Navigation 5.x installation guide tutorial and from there follow step 1, 2 and 3 to install both NavigationContainer and createStackNavigator components in your project.

2. Open our previous tutorial about Creating SQLite database and from there follow step 1 and 2 to install the React Native SQLite Storage plugin in your project.

3. After done following both above steps, Next step is to open your project’s main App.js file and import useState, useEffect, SafeAreaView, Text, View, StyleSheet, Alert, TouchableOpacity, TextInput, FlatList, openDatabase, NavigationContainer and createStackNavigator components in your project.

4. Creating a openDatabase object named as db and here we would make our SQLite database named as SchoolDatabase.db .

5. Creating our first screen component named as HomeScreen with navigation.

Explaination:

  1. S_Name : This state is used to get Student name from Text Input component.
  2. setName : Used to change S_Name state value.
  3. S_Phone : Used to get Student phone number from Text Input component.
  4. setPhone : To change S_Phone value.
  5. S_Address : Used to hold the student address from Text Input component.
  6. setAddress : To change S_Address value.
  7. useEffect() : We are using useEffect as Component did mount method and here we’re creating Table named as Student_Table in SQLite database. The table has 4 fields named as student_id, student_name , student_phone and student_address.
  8. insertData() : This function is used to enter data into SQLite database.
  9. navigateToViewScreen() : Used to navigate to next screen where we would display all the entered records in database.

6. Creating our Second screen named as ViewAllStudentScreen with navigation. In this functional component we would display all the entered records in SQLite database in FlatList component.

Explaination:

  1. items :  Items state is used to hold all the data we get from SQLite database.
  2. setItems : Used to update the items State value.
  3. empty : empty state is used to determine whether the items state is empty or not and according to that display a empty message on screen.
  4. setEmpty : To update empty state value.
  5. useEffect() : here we would execute the SELECT * FROM Student_Table query and pop up all the records form SQLite database and enter into items state.
  6. listViewItemSeparator() : To display a horizontal divider line between each Flat List item.

7. Making createStackNavigator() component object named as stack and now we would make our main export default function named as App(). Here we would make NavigationContainer component and define both screen here.

8. Creating Style.

9. Complete Source Code for App.js file:

Screenshots:

View All Data From SQLite Database in React Native FlatList Example

View All Data From SQLite Database in React Native FlatList Example View All Data From SQLite Database in React Native FlatList Example

2 Comments

  1. hii sir,
    i need your help. right know i don’t have project in my current company so i thing i wasting my time around 6 month . what can i do ?

Leave a Reply

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