I have a Sqlite database that I read from the Assets file. That was the question of how to get around one moment. Everything works well, but if you go into settings -> application -> and clear the cache and data, databases, etc., then the application throws a SQLiteException no such table: bus_end (code 1): , while compiling: SELECT * FROM because the base is no such table: bus_end (code 1): , while compiling: SELECT * FROM .

Through the file manager, I see that the database exists, but it becomes just empty, which is logical. If you slip a kick through the manager of the not jammed file, everything works further ... That is, I understand that the user can clear the cache and then everything will fall. Question How to solve this problem?

Here is my code:

 public class DatabaseHelper extends SQLiteOpenHelper { //The Android's default system path of your application database. private static String DB_PATH = ""; private static final int DB_VERSION = 1; private static String DB_NAME = "app_database.db"; private final Context myContext; private SQLiteDatabase myDataBase; public DatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); this.myContext = context; } /** * * Создает пустую базу данных в системе и переписывает ее с помощью собственной базы данных. */ public void createDataBase() throws IOException { boolean dbExist = checkDataBase(); if (!dbExist) this.getWritableDatabase();//Создает и/или открывает базу данных if (myDataBase.isOpen()){ myDataBase.close(); } try { copyDataBase(); } catch (IOException e) { throw new Error(e + "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 */ private boolean checkDataBase() { SQLiteDatabase checkDB = null; String path = DB_PATH + DB_NAME; try { checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) { //database does't exist yet } if (checkDB != null) { checkDB.close(); } return checkDB != null; } /** * 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 { //Откройте локальный db в качестве входного потока InputStream myInput = myContext.getAssets().open(DB_NAME); //Путь к только что созданному пустому db String outFileName = DB_PATH + DB_NAME; //Откройте пустой бит в качестве выходного потока 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 { String myPath = DB_PATH; myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } // ПОЛУЧИТЬ public Cursor getAllData(String table_name) { myDataBase = getReadableDatabase(); return myDataBase.query(table_name, null, null, null, null, null, null); } @Override public synchronized void close() { if (myDataBase != null) myDataBase.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

}

}

 private void retrieve() { listStationItemList.clear(); try { db.createDataBase(); db.openDataBase(); } catch (Exception e) { e.printStackTrace(); } //RETRIEVE Cursor cursor = db.getAllData(TABLE_BUS); //LOOP AND ADD TO ARRAYLIST while (cursor.moveToNext()) { long _id = cursor.getLong(0); String name = cursor.getString(1); String details = cursor.getString(2); ItemListStation p = new ItemListStation(name, details); listStationItemList.add(p); } //CHECK IF ARRAYLIST ISNT EMPTY if (!(listStationItemList.size() < 1)) { recyclerView.setAdapter(adapter); } db.close(); } 
  • First: your helper class inherits SQLiteOpenHelper , but does not execute and overrides its normal behavior. Secondly: what result do you need - so that the database is re-copied from assets in its original state after cleaning the data or that it is not deleted at all and the data in it is preserved? - woesss
  • Probably here that the base was re-copied from assets in its original form. But any of these options will do. nothing is written to my database and it will not be, the database is only for reading information output. - Romanych

0