3

Possible Duplicate:
How to use an existing database with an Android application

I have a sqlite database file, and i want to use it in my app.

How do i do it ?

How do i push my db file to the emulator, and then use it in my code ?

Any solution is welcome. thx in advance, Tom.

Community
  • 1
  • 1
user1581901
  • 65
  • 1
  • 6

2 Answers2

3
public class AssetDatabaseHelper extends SQLiteOpenHelper {

    private String dbName;
    private String db_path;
    private Context context;

    /**
     * A helper class to import db files.
     * 
     * @param base
     *            /app context
     * @param dbName
     *            The name of the db in asset folder .
     */
    public AssetDatabaseHelper(Context context, String dbName) {
        super(context, dbName, null, 1);
        this.dbName = dbName;
        this.context = context;
        db_path = "/data/data/" + context.getPackageName() + "/databases/";
    }

    /**
     * Check if the database already exist to avoid re-copying the file each
     * time you open the application.
     * 
     * @return true if it exists, false if it doesn't
     */
    public boolean checkExist() {

        SQLiteDatabase checkDB = null;

        try {
            String myPath = db_path + dbName;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {
            e.printStackTrace();
            // database does't exist yet.

        } catch (Exception ep) {
            ep.printStackTrace();
        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    /**
     * Creates a empty database on the system and rewrites it with your own
     * database.
     * */
    public void importIfNotExist() throws IOException {

        boolean dbExist = checkExist();

        if (dbExist) {
            // do nothing - database already exist
        } else {

            // By calling this method and empty database will be created into
            // the default system path
            // of your application so we are gonna be able to overwrite that
            // database with our database.
            this.getReadableDatabase();

            try {

                copyDatabase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    private void copyDatabase() throws IOException {
        InputStream is = context.getAssets().open(dbName);

        OutputStream os = new FileOutputStream(db_path + dbName);

        byte[] buffer = new byte[4096];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
        os.flush();
        os.close();
        is.close();
        this.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

}

Based off of this tutorial. You put your sqlite database file in your asset folder and it will copy when you run the code. My version allows multiply databases files since it chooses the path. To use it do :

AssetDatabaseHelper dbHelper = new AssetDatabaseHelper(
                getBaseContext(), SomeDataBase.SOME_DATABASE_NAME);
        try {
            dbHelper.importIfNotExist();
        } catch (IOException e) {
            e.printStackTrace();
        }
wtsang02
  • 18,603
  • 10
  • 49
  • 67
1

You need to extend SQLiteOpenHelper to match your DB tables. This will allow your app to interact with the DB. Check this: http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

To use the DB push it to /data/data/your.package/databases/ using the Android->File Explorer view in Eclipse while the emulator is running.

MarkMan
  • 184
  • 6
  • What about the user device, how he will `push the DB` into user device? – Yaqub Ahmad Jan 27 '13 at 12:09
  • I haven't tried push/pull directly to/from a device but the section 'Copying Files to or from an Emulator/Device Instance' linked [here](http://developer.android.com/tools/help/adb.html) offers some advice. – MarkMan Jan 27 '13 at 13:28