dimanche 19 juillet 2015

Database operation not working in Android

Hi i have precreated a database and put in folder assets.

I created a class databasehelper and within that class i created a method checkdatabase which opens database and if the operation is successful it will return 1 else 0.

then i am calling checkdatabase method from my main activity. if it returns true i just change the text of one of the text fields.

but every time i run my code to call checkdatabase method my app crashes.

following is my code:

 import android.content.Context;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteException;
 import android.database.sqlite.SQLiteOpenHelper;

 public class DatabaseHelper extends SQLiteOpenHelper{

 public static String DB_Path ="C:\\Users\\tushar\\AndroidStudioProjects\\cQuiz2\\app\\src\\main\\assets";
 private SQLiteDatabase mydatabase;
 private static String DB_NAME = "mydb";
 private final Context myContext;

 public DatabaseHelper(Context context) {

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

    SQLiteDatabase checkDB = null;

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

    }catch(SQLiteException e){
        //database does't exist yet.
    }



    return checkDB != null ? true : false;
}
@Override
public void onCreate(SQLiteDatabase db) {

}

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

    }
}

 //code to call from main activity
 DatabaseHelper d;
 if(d.checkDataBase())
    {
        txtQue.setText("true");
    }

Aucun commentaire:

Enregistrer un commentaire