Use existing Sqlite database in Android

Hello,

Since last few days, I have been working with android and I am very excited about it. This is my first blog on Android and many more is to come.

Normally Sqlite database is used with Android application. So this blog is about how you can use existing Sqlite database in Android application. First you need to add following table in your database. I recommend Sqlite  Expert Personal tool to make changes or create new Sqlite database. Open the database and run following query

“CREATE TABLE “android_metadata” (“locale” TEXT DEFAULT ‘en_US’)”

After that insert record in it.
“INSERT INTO “android_metadata” VALUES (‘en_US’)”

Once done add your database file in assets folder of your android application. You can do simple copy paste here.
Now we will create class which will have all database related functions and methods. We will use SQLiteOpenHelper class provided by Android framework to connect with Sqlite database. Following is the class definition.
public class DatabaseAdapter extends SQLiteOpenHelper {

         private static String dbPath= “data/data/com.YourPackageName/applicationDb/”;
         private static String dbName = “YourDBName”;
         private SQLiteDatabase applicationDatabase; 
         private final Context applicationContext;

       
         public VocabTesterDatabaseHelper(Context context) {
                 super(context,  dbName , null, 3);
                 this. applicationContext  = context;
         }

         private boolean checkDataBase(){
                 File dbFile = new File( dbPath +  dbName);
return dbFile.exists();
  }

          private void copyDataBase() throws IOException{
try {

                  InputStream input =  applicationContext .getAssets().open(dbName);
                           String outPutFileName=  dbPath  +  dbName ;
                     OutputStream output = new FileOutputStream( outPutFileName);
                      byte[] buffer = new byte[1024];
                  int length;
                  while ((length = input.read(buffer))>0){
                 output.write(buffer, 0, length);
                  }
                  output.flush();
                  output.close();
                  input.close();
       }
                       catch (IOException e) {
                    Log.v(“error”,e.toString());
                    }
   }

             public void openDataBase() throws SQLException{
                String fullDbPath= dbPath + dbName;
             applicationDatabase = SQLiteDatabase.openDatabase( fullDbPath,     null,SQLiteDatabase.OPEN_READONLY);
   }

                @Override
public synchronized void close() {
       if( applicationDatabase != null)
        applicationDatabase .close();
             super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

}
That’s it and your database helper class is ready. Initially we placed database in Assets folder and later on we moved it to particular folder in application. Still I am working on this class so I will add more functions to class and at the same time, I will upgrade this post. Stay tuned.
Thanks.

This entry was posted in Android, Database, Sqlite on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

15 thoughts on “Use existing Sqlite database in Android”

  1. ryan889xx

    i have a problem with this, when implementing into my app. a db is created with only the metadata table. do you know how to fix this, i think its a common error. please help

    Reply ↓
  2. Chiran Jain

    How to write a cursor method to display the data of my existing sqlite database table in a list view? Please can i get an entire code for this? I have been trying this for the past few days with no success.

    Reply ↓
  3. Anonymous

    Hi Hiren
    I am new to Android Programming. How to make sure that that code is working? What is the expected output?

    Reply ↓
  4. ha ni

    Hi,
    I am new to android and i have to retrieve data from the existing database.I used your code and i did everything as it was said here but i have a doubt, already all the tables in my database has a unique primary id ,do i have to change the name of all those primary key as _id. Please reply
    Thanks in advance.

    Reply ↓

Leave a Reply

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