Piraba
Piraba

Reputation: 7014

Android database more than 1MB

I have written database copy.I have put my database into asset forder.My db size is 4.5 MB. How to chunk the databse.

    package com.xont.db;

  public class DataBaseMasterHelper extends SQLiteOpenHelper {

// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.xont.controller/databases/";
private static String DB_NAME = "masterventura.db";
private SQLiteDatabase ventura;
private  Context myContext;
private DataBaseHelper myDbHelper;

public DataBaseMasterHelper(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 **/
public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getReadableDatabase();
        //this.getReadableDatabase().getPath();
        System.out.println( " ===== " +     this.getReadableDatabase().getPath());
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

public boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        e.printStackTrace();
    }

    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {
    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();


}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    ventura = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {
    if (ventura != null)
        super.close();
        ventura.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

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

}

In the copyDataBase() method i need to change the code.How can I split the database.Please help me on this

Thanks in advance...

Upvotes: 2

Views: 1097

Answers (1)

C Nick
C Nick

Reputation: 2520

Here is a blog post that is exactly what you are looking for:

http://www.chriskopec.com/blog/2010/mar/13/deploying-android-apps-with-large-databases/

Upvotes: 1

Related Questions