14

I'm trying to deploy an application with an existing SQLite database.

I've read through and attempted to implement several samples online however I've found that they are always missing some code and either do not compile or work as advertised.

Does anyone have a Full Android Database Helper class for deploying an existing SQLite database on Android?

Biff MaGriff
  • 8,102
  • 9
  • 61
  • 98
  • I've tried using your code, but it gets stuck on this part: ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.mdegges/.MicheleActivity. The data/data/com.mdegges/database file never gets created. – mdegges Mar 06 '12 at 20:10
  • @mdegges I'm not sure what is going wrong there. From your other questions it looks like you are also getting stuck on the LAUNCHER part. Are you able to perform a hello world test before including the db code? – Biff MaGriff Mar 06 '12 at 21:36
  • Yes, I've been able to complete plenty of tuts (including hello world) on the dev site. It's so strange that none of the guides I've tried have worked.. The db is in my assets folder, I changed DB_PATH to the correct output folder, and I changed db name to my db (with and without an extension), but no luck! – mdegges Mar 06 '12 at 21:55
  • I was able to get it working. :) – mdegges Mar 11 '12 at 07:03
  • This is basically asking "give me a database helper class for SQLLite" and fits the definition of "overly broad". And I didn't downvote, I *closed* the question. Questions closed under NARQ are automatically given a -1 vote as a result of the closing. The answer is good, but the question itself is bad and needs a lot of work. Asking and answering your own questions is ok (and even encouraged) but that places double duty on you, to provide a quality answer *and* a quality question. – casperOne Nov 28 '12 at 20:01

2 Answers2

27

This is what I came up with, hope it helps others that were having troubles.

package com.MyPackage;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class AnyDBAdapter {

    private static final String TAG = "AnyDBAdapter";
    private DatabaseHelper mDbHelper;
    private static SQLiteDatabase mDb;

    //make sure this matches the 
    //package com.MyPackage;
    //at the top of this file
    private static String DB_PATH = "/data/data/com.MyPackage/databases/";

    //make sure this matches your database name in your assets folder
    // my database file does not have an extension on it 
    // if yours does
    // add the extention
    private static final String DATABASE_NAME = "data";

    //Im using an sqlite3 database, I have no clue if this makes a difference or not
    private static final int DATABASE_VERSION = 3;

    private final Context adapterContext;

    public AnyDBAdapter(Context context) {
        this.adapterContext = context;
    }

    public AnyDBAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(adapterContext);

        try {
            mDbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        try {
            mDbHelper.openDataBase();
        } catch (SQLException sqle) {
            throw sqle;
        }
        return this;
    }
    //Usage from outside
    // AnyDBAdapter dba = new AnyDBAdapter(contextObject); //in my case contextObject is a Map
    // dba.open();
    // Cursor c = dba.ExampleSelect("Rawr!");
    // contextObject.startManagingCursor(c);
    // String s1 = "", s2 = "";
    // if(c.moveToFirst())
    // do {
    //  s1 = c.getString(0);
    //  s2 = c.getString(1);
    //  } while (c.moveToNext());
    // dba.close();
    public Cursor ExampleSelect(string myVariable)
    {
        String query = "SELECT locale, ? FROM android_metadata";
        return mDb.rawQuery(query, new String[]{myVariable});
    }

    //Usage
    // AnyDBAdatper dba = new AnyDBAdapter(contextObjecT);
    // dba.open();
    // dba.ExampleCommand("en-CA", "en-GB");
    // dba.close();
    public void ExampleCommand(String myVariable1, String myVariable2)
    {
        String command = "INSERT INTO android_metadata (locale) SELECT ? UNION ALL SELECT ?";
        mDb.execSQL(command, new String[]{ myVariable1, myVariable2});
    }

    public void close() {
        mDbHelper.close();
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {

        Context helperContext;

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            helperContext = context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database!!!!!");
            //db.execSQL("");
            onCreate(db);
        }

        public void createDataBase() throws IOException {
            boolean dbExist = checkDataBase();
            if (dbExist) {
            } else {

                //make sure your database has this table already created in it
                //this does not actually work here
                /*
                 * db.execSQL("CREATE TABLE IF NOT EXISTS \"android_metadata\" (\"locale\" TEXT DEFAULT 'en_US')"
                 * );
                 * db.execSQL("INSERT INTO \"android_metadata\" VALUES ('en_US')"
                 * );
                 */
                this.getReadableDatabase();
                try {
                    copyDataBase();
                } catch (IOException e) {
                    throw new Error("Error copying database");
                }
            }
        }

        public SQLiteDatabase getDatabase() {
            String myPath = DB_PATH + DATABASE_NAME;
            return SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        }

        private boolean checkDataBase() {
            SQLiteDatabase checkDB = null;
            try {
                String myPath = DB_PATH + DATABASE_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READONLY);
            } catch (SQLiteException e) {
            }
            if (checkDB != null) {
                checkDB.close();
            }
            return checkDB != null ? true : false;
        }

        private void copyDataBase() throws IOException {

            // Open your local db as the input stream
            InputStream myInput = helperContext.getAssets().open(DATABASE_NAME);

            // Path to the just created empty db
            String outFileName = DB_PATH + DATABASE_NAME;

            // Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(outFileName);

            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }

        public void openDataBase() throws SQLException {
            // Open the database
            String myPath = DB_PATH + DATABASE_NAME;
            mDb = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        }

        @Override
        public synchronized void close() {

            if (mDb != null)
                mDb.close();

            super.close();

        }
    }

}
Biff MaGriff
  • 8,102
  • 9
  • 61
  • 98
  • 1
    With your class, it is possible to open a database that resides on the SD card? – Thiago Jul 20 '11 at 19:51
  • If you have an existing db that you copied yourself to your SD card I guess you could try removing the `copyDataBase()` call and changing the DB_PATH to the appropriate location. But that is just a guess. I'd rather not let the user have direct access to the app data so I never considered that option. – Biff MaGriff Jul 20 '11 at 21:47
  • Looks like a typo on my part. I corrected the code to `helperContext`. Good catch :) – Biff MaGriff Jul 26 '11 at 19:39
  • I'm also a bit lost on the commented example line `AnyDBAdapter dba = new AnyDBAdapter(contextObject);` can I use this to get the results of that into a List or ArrayList? I don't know what I need to use for contextObject – Carnivoris Jul 26 '11 at 19:52
  • I believe it is a reference to your activity. I use it like so `public class MyClass extends MapActivity { public void doDBStuff() { new AnyDBAdapter(this).doDBThing(); } }` – Biff MaGriff Jul 26 '11 at 20:23
  • 2
    The database version "DATABASE_VERSION" is the current version of your developed database.. so if you work on the 1st version of app the database should have V1, if you update the app to version 2 and update the database too, the database version should have v2 and so on.. this flag is used to get track of the database updates and if android detects that the database version is increased, it calls a onUpdate() method that can let you backup the current database already stored on the user phone so it wont get deleted and replaced with the new one... – Cata Aug 01 '12 at 06:17
  • Sorry, I think I should know this but: in public synchronized void close() {...} why is the IF only affecting mDb.close(); and not super.close(); ? @BiffMaGriff – fersarr Jul 16 '14 at 23:22
  • What is the difference between calling `AnyDBAdapter.close()` and `SQLiteDatabase.close()`? – aandis Mar 15 '15 at 09:28
5
DatabaseHelper dbHelper = new DatabaseHelper(getApplicationContext());

Make sure you create DatabaseHelper object once during application lifetime and reuse it.

For reading data:

SQLiteDatabase db = dbHelper.getReadableDatabase();

For reading/modification data:

SQLiteDatabase db = dbHelper.getWritableDatabase();

next use insert(), query(), update(), delete() methods of SQLiteDatabase object:

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

You also should not create the database by directly accesssing the sqlite file what you do in createNewDatabase method. Use execSQL() method of SQLiteDatabase object in your onCreate(...) method. Execute your CREATE TABLE queries there.

Czechnology
  • 14,832
  • 10
  • 62
  • 88
plugmind
  • 7,926
  • 4
  • 34
  • 39
  • Hi radek-k, would you be able to expand on your first point? `Make sure you create DatabaseHelper object once during application lifetime and reuse it.` – Biff MaGriff Aug 23 '10 at 19:30
  • 1
    If you create DatabaseHelper object many times make sure you closed database previously - getWritableDatabase.close(). In a multi threaded application model you cannot recreate DatabaseHelper when you didn't close previous one. You will simply get some Exceptions. The best approach is to create DatabaseHelper object once only (in Application for example) and use the same object reference all the time. – plugmind Aug 23 '10 at 19:46
  • 1
    I would not recommend the use of `getApplicationContext()`. Use this instead, since whatever is responding to `getApplicationContext()` is a `Context`. – CommonsWare Aug 23 '10 at 21:51
  • You can use `this` inside `Activity`, `Application`. Commonsware and I we both are right. Just take a look at api docs and pass anything which extends `android.content.Context`. http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#SQLiteOpenHelper%28android.content.Context,%20java.lang.String,%20android.database.sqlite.SQLiteDatabase.CursorFactory,%20int%29 – plugmind Aug 25 '10 at 09:35