waqasbhalli345
waqasbhalli345

Reputation: 19

getting error copying database from asset folder on real device and emulator

My app crashed and getting this exception in logcat. But when I check my database folder in DDMS I see file with data copied there but not showing in my listview. The problem is with createDatabase() function and getting exception from there. Please see the below logcat stack trace and my code. Please help I am stucking badly and don't know what to do.

04-30 16:04:32.382: E/AndroidRuntime(20086): FATAL EXCEPTION: main

04-30 16:04:32.382: E/AndroidRuntime(20086): java.lang.Error: Error copying database

04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.WaqasAsgharBhalli.za_traders.DatabaseHandler.createDatabase(DatabaseHandler.java:142)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.WaqasAsgharBhalli.za_traders.DatabaseHandler.<init>(DatabaseHandler.java:121)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.WaqasAsgharBhalli.za_traders.MainActivity.onCreate(MainActivity.java:27)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.Activity.performCreate(Activity.java:4465)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread.access$600(ActivityThread.java:122)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.os.Looper.loop(Looper.java:137)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread.main(ActivityThread.java:4340)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at java.lang.reflect.Method.invokeNative(Native Method)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at java.lang.reflect.Method.invoke(Method.java:511)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at dalvik.system.NativeStart.main(Native Method)

DatabaseHandler.class

 public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables

private static String TAG = DatabaseHandler.class.getName();

// Database Path
 private static String DB_PATH = "/data/data/com.WaqasAsgharBhalli.za_traders/databases/";
  private static String DB_NAME = "myDatabase.sqlite";
  private SQLiteDatabase myDataBase = null; 
  private final Context myContext;

// Database Version
private static final int DATABASE_VERSION = 1;


public DatabaseHandler (Context context) throws IOException  {
    super(context,DB_NAME,null,1);
    this.myContext=context;
    boolean dbexist = checkDatabase();
    if(dbexist)
    {
        //System.out.println("Database exists");
        openDatabase(); 
    }
    else
    {
        System.out.println("Database doesn't exist");
    createDatabase();
    }

}

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */

public void createDatabase() throws IOException{
    boolean dbexist = checkDatabase();
    if(dbexist)
    {
        //System.out.println(" Database exists.");
    }
    else{
        this.getReadableDatabase();
    try{
            copyDatabase();
        }
        catch(IOException e){
            throw new Error("Error copying database");
        }
    }
}

/**
 * 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 checkDatabase() {
    //SQLiteDatabase checkdb = null;
    boolean checkdb = false;
    try{
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        //checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
        checkdb = dbfile.exists();
    }
    catch(SQLiteException e){
        System.out.println("Database doesn't exist");
    }

    return checkdb;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDatabase() throws IOException {

    //Open your local db as the input stream
    InputStream myinput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outfilename = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myoutput = new FileOutputStream("/data/data/com.WaqasAsgharBhalli.za_traders/databases/myDatabase.sqlite");

    // transfer byte to inputfile to 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 + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);

}

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

MainActivity.class

    DatabaseHandler myDbHelper = null;
    try {
        myDbHelper = new DatabaseHandler(this);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        throw new Error("Unable to create database");
    }

Upvotes: 0

Views: 697

Answers (1)

Simulant
Simulant

Reputation: 20112

I advice you to use a library to do the copying of a database in Android. The are a lot of pit-falls like different default path locations for different Android versions. I used this library in my project to copy a database and it works perfectly fine.

Upvotes: 1

Related Questions