Мое приложение хорошо работает на виртуальном устройстве под управлением Android версии 9, но на моем телефоне и других устройствах (под управлением версий 7, 8, ...) я получаю эту ошибку:
Caused by: android.database.sqlite.SQLiteException: no such table: category (code 1): , while compiling: SELECT ....
Может кто-то помочь мне, пожалуйста?
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 5;
// Database Name
private static final String DATABASE_NAME = "quotes.db";
private static final String DB_PATH_SUFFIX = "/databases/";
// Quotes table name
private static final String TABLE_QUOTES = "quote";
// Quotes Table Columns names
private static final String KEY_ID = "_id";
static Context myContext;
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
myContext = context;
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.setVersion(oldVersion);
}
public void CopyDataBaseFromAsset() throws IOException {
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = getDatabasePath();
// if the path doesn't exist first, create it
File f = new File(myContext.getApplicationInfo().dataDir
+ DB_PATH_SUFFIX);
if (!f.exists())
f.mkdir();
// 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();
Log.e("copydb","seccess");
}
private static String getDatabasePath() {
return myContext.getApplicationInfo().dataDir+DB_PATH_SUFFIX+DATABASE_NAME;
}
public void openDataBase() throws SQLException {
File dbFile = myContext.getDatabasePath(DATABASE_NAME);
if (!dbFile.exists()) {
try {
CopyDataBaseFromAsset();
Log.e("copydb","Copying sucess from Assets folder");
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DataBaseHandler.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
if ( newVersion>oldVersion){
try {
CopyDataBaseFromAsset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.w(DataBaseHandler.class.getName(), "Data base is upgraded ");
}
}