Published on Feb 22,2017
Email Post

Introduction on Android SQLite Database

The Android SQLite Database requires very little memory (around 250kb), which is available on all android devices. Every device has an inbuilt support for SQLite database, which is automatically managed on android right from its creation, execution to querying up process.

SQLite is an open source database, available on every android database. It supports standard relations database features, like SQL syntax, transactions & SQL statements. SQLite is considerably, the lighter version of SQL database, where most of the SQL commands don’t run on SQLite database. Once SQLite is in place, it is important to ensure that a feature or command is available in SQLite; only then can it be executed.

The Basic Advantages of SQLite:

  • It’s a light weight database
  • Requires very little memory
  • An Automatically managed database

The SQLite supports only 3 Datatypes:

  • Text(like string) – for storing data type store
  • Integer(like int) – for storing integer primary key
  • Real(like double)- for storing long values

Basically SQLite does not validate datatypes by itself. In other words, whatever datatypes are used, they are termed as valid.

For example, in this case, the database of a cable operator has been discussed. Here, a new table is added with ‘text’ in the name field and fieldname box carrying ‘textfield’. A random value datatype has been created. The end result is a test table with an invalid datatype, which shows that SQLite doesn’t validate datatype.

*One can find the database by clicking on package and accessing the data folder to the database folder, which contains the file.

Note: The SQLiteOpen Helper class is used to manage database creation and version management.

User Questions:

Does the change in db version result is change of SQLite version of the object?

There is no change in the SQLite version of the object since one handling creation/update of database, and the other is playing on data held into the tables of the database. Thereby there is no dependency at all

Coming back, when we extend SQLiteopenHelper class, we overwrite the Oncreate & OnUpgrade methods in this class. When the SQLite database is accessed, it can be slow sometimes, depending on the complexity of the query. It is always preferred to make sure that the least impact or queries running in recursion are avoided when an SQL query is executed. Also it is  recommended to perform database synchronously. These methods are automatically handled or called by the framework. Also, it must be noted that whenever one increases the DB version, it will automatically call onUpgrade on to it.

Whenever one talks about SQLite open helper class, its database name & version gets passed in the constructor of extended class.

Here, two things are to be taken into account:

  • OnCreate() method is called, if the database does not exist.
  • OnUpgrade() method is called, if the database version is updated.

The SQLite open helper class gives the name and version in the constructor. The getwriteabledatabase() is called and if the database does not exist, it will OnCreate. If in case, the database exists and version in updated, it will call OnUpgrade().

Are these methods on firstcall of db?

Let us take an example. Once we install an application, it will create OnCreate and the method apk is uploaded onto Google play store. Once the latest version of the apk is downloaded, it will check the database already installed. If it has a higher version, it will call upgrade, if not, then it will do nothing.

User Question

Is it required to have user permission to upgrade db version?

No, the permission is not required, since we are working on the application resources we don’t have to seek permission.

User Question

If user1 has installed my application and it is an upgraded version for DB and subsequently user2 is installing new application and the user1 is upgrading the app. In such a scenario, if user1 on version upgrade is called and for user2 create method is called, are all the methods from android?

Yes, because we are not having any version already installed on to the device. It doesn’t matter, as long as there is no earlier version installed into the device. It’s very much like framework handling.

User Question

Can we have the SQLite database object? What if my db name has conflict with another db name object?

It doesn’t matter, because database resides on the application’s package name, which should not be conflicting even if a sub-folder in the same name has been created. It only matters on root level and the root level handling is done with the help of the package name. It must be noted that we can’t have two applications with the same package name, but we can have 2 different db names under different packages. SQLite database takes care of updation, deletion, insertion, querying up, and closing and opening of database operations with the help of the object.

Got a question for us? Mention them in the comments section and we will get back to you.

Related Posts:

Beginner’s Guide to Android Architecture

Android Development Course

About Author
Published on Feb 22,2017

Share on

Browse Categories