Skip to content

Android, SQLite and a few Problems

Usually the above error message indicates that the SQLite database was not initialized, meaning it is empty. There are two ways to initialize an SQLite database:

  1. In your code you can define CREATE statements that create the tables and then use INSERT to populate it with data
  2. Use a preconfigured database that you import in your Android project.

The latter can be done by copying an existing SQLite database from your asset folder to your Android database folder which usually can be found at /data/data/com.whatever.worksforyou/databases/database_name.sqlite
Note that you don’t have access to these folders on a real non-rooted device but you can explore them using the emulator. There are numerous tutorials on the internet that describe how to ¬†copy the database (e.g.¬†http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/).

But there are two problems that you may encounter when dealing with SQLite database.

  1. File size is over 1MB – If you target Android versions prior 2.3 than you will run into problems when your prepopulated database in your asset folder is larger than 1MB. The whole topic is described in this post. The easiest solution to this problem is to rename your database’s extension to an extension that Android will not try to compress because these file formats already contain compressed data and can not be compressed further(e.g. JPG, GIF,WAV, etc). I usually chose the JET extension because JET is a known database format and I rather use a wrong database file extension than using an extension that has nothing to do with databases.
    But keep in mind that file sizes on a mobile device should not grow too big due to memory and performance reasons.
  2. No such table – Now, as I mentioned before you most probably will get this error message when the copy process from the asset to the database folder did not work correctly. But recently I got this error where I was sure that the copy process completed correctly. Even stranger was the fact that my code worked on most of the devices but not on some. In my case it worked on the Google Nexus S, the B&N Nook Color but not on the B&N Nook Tablet.
    The problem lies in the copy method.

    getReadableDatabase();

    The method above returns a database object which equals an empty database. On some devices it is necessary to close the database otherwise the database remains empty even after the copy process.

    getReadableDatabase();
    close();

    The above code calls the DBHelper’s close method which enables copying the data into the database.

3 Comments

  1. ramesh

    Hi,

    I get the same error(No such table) only for Nook Tablet.Can you please send the sample code for regarding this

Leave a Reply

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

*