dimanche 24 janvier 2016

Not able to copy the existing Database in Android

I am new to Android development,So i have an existing database called Courses,and DatabaseHelper file which will copy the existing file from assets folder in Android studio but it runs the loop and creates an empty database with android_manifest file in it ,"No TABLES ARE COPIED".So therefore giving me and error of No table found when running a select table.

Error: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.sinhanikita17.myapplication, PID: 15127 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sinhanikita17.myapplication/com.example.sinhanikita17.myapplication.Choose}: android.database.sqlite.SQLiteException: no such table: ICS (code 1): ,

public class DatabaseHelper extends SQLiteOpenHelper { private static String DB_PATH;

private static String DB_NAME = "Courses.db";

private SQLiteDatabase myDataBase;

private final Context myContext;

private static final String LOGTAG = "Myapp5";
private static DatabaseHelper _dbHelper;

public DatabaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;


        DB_PATH =  "/data/data/" + context.getPackageName() + "/databases/";
    Log.i(LOGTAG, DB_PATH);
}

public static DatabaseHelper getInstance(Context context)
{
    if(_dbHelper == null)
    {
        _dbHelper = new DatabaseHelper(context);
    }
    return _dbHelper;
}

public void createDataBase() throws IOException{


    boolean dbExist = checkDataBase();

    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 boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

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

    }catch(SQLiteException e){

        //database doesn't exist yet.

    }

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

    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open("Courses.db");
    Log.i(LOGTAG, String.valueOf(myContext.getAssets()));

    // 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(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();

}

Aucun commentaire:

Enregistrer un commentaire