Почему-то не находит таблицу bludas в БД, хотя я создала таблицу такую в БД.. Подскажите пожалуйста почему
Вот мой класс DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
private static String DB_PATH;
public static final String DB_NAME = "bluda.db";
public static final int DB_VERSION = 1;
public SQLiteDatabase database;
private Context myContext;
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
try {
DB_PATH = myContext.getDatabasePath(DB_NAME).toString();
createDataBase();
openDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
public Cursor getBluda() {
return database.query(
Contract.Bluda.TAB_BLUDA,
null,
null,
null,
null,
null,
null);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
}
catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);
}
catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
database = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(database != null)
database.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Класс Contract.java
public final class Contract {
private Contract() {}
public static abstract class Bluda implements BaseColumns {
public static final String TAB_BLUDA = "bludas";
public final static String _ID = BaseColumns._ID;
public static final String COL_BLUDO = "bludo";
public static final String COL_RECEPT = "recept";
public static final String COL_LIKE = "like";
}
}
Выдает вот такую ошибку
FATAL EXCEPTION: ModernAsyncTask #1 Process: ru.test.appbluda, PID: 21019 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:161) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:762) Caused by: android.database.sqlite.SQLiteException: no such table: bludas (code 1): , while compiling: SELECT * FROM bludas
########################################################### Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing
database. (no such table: bludas (code 1): , while compiling: SELECT * FROM bludas)
########################################################### at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1008) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:573) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:59) at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1711) at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1558) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1429) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1597) at ru.test.appbluda.db.DBHelper.getBluda(DBHelper.java:38) at ru.test.appbluda.MainActivity$MyCursorLoader.onLoadInBackground(MainActivity.java:84) at ru.test.appbluda.MainActivity$MyCursorLoader.onLoadInBackground(MainActivity.java:68) at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57) at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:45) at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:138) at java.util.concurrent.FutureTask.run(FutureTask.java:237) ... 3 more