How to Create and Add Data to SQLite Database in Android?
Last Updated :
13 Mar, 2024
SQLite is another data storage available in Android where we can store data in the user’s device and can use it any time when required. In this article, we will take a look at creating an SQLite database in Android app and adding data to that database in the Android app. This is a series of 4 articles in which we are going to perform the basic CRUD (Create, Read, Update, and Delete) operation with SQLite Database in Android. We are going to cover the following 4 articles in this series:
- How to Create and Add Data to SQLite Database in Android?
- How to Read Data from SQLite Database in Android?
- How to Update Data to SQLite Database in Android?
- How to Delete Data in SQLite Database in Android?
What is SQLite Database?Â
Android SQLite Database is an open-source database provided in Android that is used to store data inside the user’s device in the form of a Text file. We can perform many operations on this data such as adding new data, updating, reading, and deleting this data. SQLite is an offline database that is locally stored in the user’s device and we do not have to create any connection to connect to this database. Â
How Data is Being Stored in the SQLite Database?Â
Data is stored in the Android SQLite database in the form of tables. When we store this data in our SQLite database it is arranged in the form of tables that are similar to that of an Excel sheet. Below is the representation of our SQLite database which we are storing in our SQLite database.Â
Important Methods in SQLite Database
Below are the several important methods that we will be using in this SQLite database integration in Android.Â
|
getColumnNames() | This method is used to get the Array of column names of our SQLite table. |
getCount() | This method will return the number of rows in the cursor. |
isClosed() | This method returns a Boolean value when our cursor is closed. |
getColumnCount() | This method returns the total number of columns present in our table. |
getColumnName(int columnIndex) | This method will return the name of the column when we passed the index of our column in it. |
getColumnIndex(String columnName) | This method will return the index of our column from the name of the column. |
getPosition() | This method will return the current position of our cursor in our table. |
What we are going to build in this article?Â
We will be building a simple application in which we will be adding data to the Android SQLite database. We will be creating a database for adding course name, course description, course duration, and course tracks. We will be saving all this data in our Android SQLite database. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.Â
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Adding permissions to access the storage in the AndroidManifest.xml file
Navigate to the app > AndroidManifest.xml and add the below code to it. Â
XML
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />