0

I'm trying to make an dataAdapter extend SQLiteOpenHelper. Bellow is the code:

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

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import com.muoiot.enlistening.idefined.*;
import com.muoiot.enlistening.utilities.MsgUtils;

public class DbAdapter extends SQLiteOpenHelper implements DB_INFO, DB_USER, DB_QUESTION {

    public static String defaultDbPath = "";
    protected SQLiteDatabase databaseControl;
    private Context context = null;

    public DbAdapter(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.context = context;
        defaultDbPath = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases/";
    }

    /***********************************************************************************/
    // This function auto call at the first time when the database was not found.
    /***********************************************************************************/   
    @Override
    public void onCreate(SQLiteDatabase db) {

        MsgUtils.DB("Don't have database, need to copy from asset");
        try {
            copyDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    /***********************************************************************************/
    // This function auto call when the database has been opened.
    /***********************************************************************************/   
    @Override
    public void onOpen(SQLiteDatabase db){
        MsgUtils.DB(" AAAAAAAAAAAAAAAAAAAAAAAAA has been opened!!!");
    }

    /***********************************************************************************/
    // Copy database
    /***********************************************************************************/       
    private void copyDataBase() throws IOException {

        // Open your local db as the input stream
        InputStream myInput = this.context.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = defaultDbPath + DB_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();

    }

    /***********************************************************************************/
    // Open and close database 
    /***********************************************************************************/   
    public void openDatabase(){
        databaseControl = this.getWritableDatabase();
    }

    public void closeDatabase(){
        this.getWritableDatabase().close();
    }

}

And, now I have an issue with copyDataBase function. I always get an database with out any table. (my phone was rooted, so I copy back the database from data/data//databases to review).

What am I wrong here? Anyone can give me a hand?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32
  • Please consider using `SQLiteAssetHelper`, which already does all of this for you: https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare Jul 06 '13 at 11:00
  • Hello, thanks for your help. I'll try it. But i'm trying to figure out what am i wrong here? :( why does it create a database with the same size with my database but without any table? Do you have any idea, please! – Lạng Hoàng Jul 06 '13 at 11:10
  • possible duplicate of [Database not copying from assets](http://stackoverflow.com/questions/5945196/database-not-copying-from-assets) – k3b Jul 06 '13 at 11:41
  • @k3b: yep, i see it, thank you! I'm not only find the solution but also find the answer for my code part, why doesn't it work? (I'll check the solution from the topic and try to figure out what am i wrong) – Lạng Hoàng Jul 06 '13 at 12:32

1 Answers1

0

AFAIK, the function onCreate() only call at the first time we access to database and the database isn't exist. So i think that put some code here to copy database is ok. But look like there are something... that i'm not clearly at this time.

When i try to put my code to this function (copy database from data asset), the database was created have the same size with mine but there are no table.

I just follow the solution that mentioned at Database not copying from assets. And it working properties now! I just see only one different thing that he call copy database in constructor instead OnCreate and he call getReadableDatabase() at first.

Anyway, i have to complete what i have started then come back to this topic since i'm not satisfy with the answer. If some one have the same issues, try to use the solution at Database not copying from assets to solve first. If you can figure out what am i wrong, please help me!

Many thanks for all of you!

Community
  • 1
  • 1
Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32