vendredi 24 avril 2015

Why other tables did not copied while android_metadata did?

[Newbie] I use raw database file (.sqlite format) as asset file contain 3 tables (android_metadata, categories, crops) and use my phone as debugging device. After complete debugging, I check my database in my phone through Android Device Monitor, use 'Pull a file from the device' feature to read my database. android_metadata is exist, but not with two others. Here's my complete code, please correct wherever it's needed.

DatabaseHelper

public class DatabaseHelper extends SQLiteOpenHelper {

private static String DB_PATH= "data/data/com.mundane.hortipedia/assets/databases/";
private static String DB_NAME = "horticulture.sqlite";
private SQLiteDatabase myDB;
private final Context context;


public DatabaseHelper(Context context) throws IOException {
    super(context,  DB_NAME , null, 3);
    this.context  = context;
    boolean dbexist = checkDB();
    if(dbexist){
        openDB();
    }else{
        System.out.println("Database doesn't exist");
        createDB();
    }
}


public void createDB() throws IOException {
    this.getReadableDatabase();
    Log.i("Readable ends...", "end");

    try {
        copyDB();
        Log.i("Copy db ends...","end");
    } catch (IOException e) {
        throw new Error("Error copying database");
    }
}

private boolean checkDB(){
    boolean checkDB = false;
    try{
        String myPath = DB_PATH + DB_NAME;
        File dbFile = new File(myPath);
        SQLiteDatabase.OPEN_READONLY);
        checkDB = dbFile.exists();
    } catch(SQLiteException e){
        System.out.println("Database doesn't exist");
    }
    return checkDB;
}

public void copyDB() throws IOException{
    try {
        Log.i("inside copyDB...","start");
        InputStream myInput =  context.getAssets().open(DB_NAME);
        Log.i("Input Stream....",myInput+"");
        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);
            Log.i("Content.... ",length+"");
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();
    } catch (IOException e) {
        Log.v("error", e.toString());
    }
}

public void openDB() throws SQLiteException {
    String myPath = DB_PATH + DB_NAME;
    myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    Log.i("open DB......",myDB.toString());
}

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


@Override
public void onCreate(SQLiteDatabase db) {
}

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

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.fragment_drawer);

    // Set up drawer
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);

    try {
        myDbHelper = new DatabaseHelper(this);
        myDbHelper.createDB();
    } catch (IOException e) {
        throw new Error("Unable to create database");
    }

    myDbHelper.openDB();

}

Aucun commentaire:

Enregistrer un commentaire