SQLite: Android

Man, things have been busy, since I had to find out how to implement SQLite in both iOS and Android, which was also my first experience of creating database back-ends outside of PHP + MySQL. However, since people other than myself have had troubles with SQLite on Android, today, I thought I would go over how to get it to work.

No! Please do not give us another SQLite for Android tutorial. Are you going to give us the same content as the others? Fortunately, I do not plan to do a how-to post on this subject. There are plenty around the Internet, of which I recommend the ones done by thenewboston on Youtube. In fact, that was what helped me get SQLite working in my own Android app. As there are plenty of SQLite for Android tutorials, I will not be one myself.

If you are not doing a how-to, then why cover this topic? After I successfully I got an SQLite back-end established via FMDB in iOS, I decided to do the same in Android. However, when I went about searching on how to implement it, I could not get the SQLiteOpenHelper class, which Google suggests using in Android, to work. Some time later, I discovered that unlike iOS, Android only creates databases when a call to SQLite is made, so creating an object of SQLiteOpenHelper is not going to create the database automatically. On the other hand, my app was constantly crashing when I did initiate a call to SQLite via the SQLiteOpenHelper. Also, many people were wondering why the onCreate method was not being issued, from my own searching on this topic. For this reason, I decided to cover this topic. SQLite on Android cannot be achieved in the same fashion as FMDB on iOS.

How did you get SQLiteOpenHelper to work? After much time searching, I came across the tutorials done by thenewboston on Youtube. His tutorials were easy to understand, but it took me a while to find the right video. Much like Google, on their developer page, and unlike many tutorials on SQLite out there, he had only the onCreate and onUpgrade methods in the SQLiteOpenHelper class written up. All of the rest of the SQL operations were done in the custom super class, though I just separated them into separate files. After that, I took the suggestion in the video and created an open method in my database manager class that created an object of my own class database helper class, which inherited from SQLiteOpenHelper, as well as opened a connection to SQLite. Another suggestion I took from the tutorials was to also create a close method in the database manager class, which issues a call to close the SQLiteOpenHelper class. After doing all of that and editing my Activity's main source to use the open and close methods of the database manager class, my Android app worked like a charm. Even though the first call to SQLite initiates the onCreate method of SQLiteOpenHelper, the app will not work right unless there are open and close methods to use with the connection.

You did not put in any source code samples. How can we believe you are telling us the truth or how to implement your finding? Sorry about that, but I do not want this post to be too long. If you want to see an example, my app's GitHub page should be able to help.

While Google recommends making a class that inherits from SQLiteOpenHelper, the database will not be created without an initial call to SQLite, which may not work without a class to communicate between the SQLiteOpenHelper and the Activity, with open and close methods included.

Did this help you get SQLite working on Android? Do you have any other solutions? Did I get anything wrong? Feel free to comment.

Use an app on your phone (e.g. Scan for Android) to capture the image above. If successful, you should be taken to the web version of this article.

Copyright © 2015 Bryce Campbell. All Rights Reserved.