Android SQLite Database for Android Developers
In this tutorial, we’ll learn about Android SQLite Database. Till now, we’ve already read the different ways to store the data in Android like internal storage, external storage, and preferences. This technique is a useful option for the storage of small quantities of data. Thus, to store, maintain, and retrieve large amounts of data, we’ll prefer the SQLite database in Android. So, before we start with SQLite in Android, we’ll understand what SQLite is.
Do you want to become an Android Professional? Join Android Telegram Channel
What is SQLite?
SQLite is basically a Relational Database Management System (RDBMS), same as SQL. It is an open-source in-process library that is self-contained, serverless, has zero-configuration, and a transactional SQL database engine. Here, zero-configuration means unlike other database management systems, it doesn’t need to be configured on the devices. The Lite here in SQLite is in terms of its setup, the database administration, and all the required resources.
Features of Android SQLite
SQLite has many features which it supports as follows:
- Full-featured SQL implementation along with various advanced capabilities such as partial indexing, JSON, and some other.
- Very simple with easy to use API.
- Has a really fast execution at times, and it is even faster than the direct file system Input-Output.
- Self-dependent as it has no external dependencies.
- Transactions in SQlite also have the ACID property that is – Atomicity, Consistency, Isolation, and Durability.
- Cross-platform as it supports Android, iOS, Linux, Mac, Windows, VxWorks, Solaris. It is easy to port it to other systems.
- Comes along with a standalone command-line interface client.
- Completely stored in a single cross-platform.
- Written completely in ANSI-C.
- Highly lightweight and small, as it is possible to configure it in less than 400Kbs. If configured by omitting the optional features, it uses less than 250Kbs.
SQLite in Android
So, we know that SQLite is an open-source RDBMS used to perform operations on the databases stored in the form of rows and columns. SQLite is highly supported by Android; in fact, Android comes with the built-in database implementation of SQLite. It is available on each and every Android database and emphasizes scalability, centralization, concurrency, and control. This strives to provide storage at the local level for applications and devices. It is highly economical, efficient, reliable, and independent. It is very simple and doesn’t need to be compared with client/server databases.
1. SQLite supports the following three types of data:
- Text Type – to store strings or character type data.
- Integer Type – to store the integer data type.
- Real Type – to store long values.
In SQLite, the data types that are used are termed as valid; it is not validated by SQLite.
2. To use SQLite in Android applications, we use the package android.database.sqlite. This package contains all the APIs to use SQLite.
3. SQLiteOpenHelper is a class that is useful to create the database and manage it. The two constructors of SQLiteOpenHelper class are:
- SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version_no) – It creates objects for creating, opening, and managing the database.
- SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version_no, DatabaseErrorHandler errorHandler) – Its object specifies the error handler along with creating, opening, and managing the database.
4. SQLiteDatabase is a class that has methods to perform operations such as create, update, delete, etc.
There are many methods that it includes, and a few of them are as follows :
|Void execSQL(String sql_query)||It will execute SQLquery.|
|Long insert(String table_name, String nullColumnHack, ContentValues values)||It will insert the record in the table with the values that are passed. The second argument makes sure that no null value is allowed._name|
|Int update(String table_name, ContentValues values, String where_clause, String where_args )||It will update the a that satisfies the where clause.|
Finally, in this Android tutorial, we read about the SQLite in Android. We saw what SQLite is and what are some important features of SQLite. We then went through the reasons we use it in Android. Then we saw its important methods and what their functions are with their brief description.
Thank you and stay tuned with us for more tutorials on Android.