3

We are getting the SQLite exception of "No such Table found". This issue is only reported on the device with brand ZTE(Z982 & N95630). On all other device the DB is working fine so not able to reproduce. Is ther issue with the Database path?

Below is the SQLite Helper class that we are using

public ExternalDbOpenHelper( Context context, String databaseName ) {
    super( context, databaseName, null, DB_VERSION );
    this.context = context;
    String packageName = context.getPackageName();
    DB_PATH = String.format( "//data//data//%s//databases//", packageName );
    DB_NAME = databaseName;

    openDataBase();
}

Method to create Database

public void createDataBase() {
    boolean dbExist = checkDataBase();
    if ( dbExist ) {
        Log.i( this.getClass().toString(), "Database already exists" );
        try {
            this.getWritableDatabase();
        }
        catch ( SQLiteReadOnlyDatabaseException sqr ) {
            this.getWritableDatabase();
            sqr.printStackTrace();
        }
    }
    else if ( !dbExist ) {
        try {
            this.getWritableDatabase();
        }
        catch ( SQLiteReadOnlyDatabaseException sqr ) {
            this.getWritableDatabase();
            sqr.printStackTrace();
        }
        try {
            copyDataBase();
        }
        catch ( IOException e ) {
            e.printStackTrace();
            Log.e( this.getClass().toString(), "Copying error" );
        }
    }
}

Datbase exists or not

private boolean checkDataBase() {
    SQLiteDatabase checkDb = null;
    try {
        String path = DB_PATH + DB_NAME;
        File   file = new File( path );
        if ( file.exists() && !file.isDirectory() ) {
            checkDb = SQLiteDatabase.openDatabase( path, null, SQLiteDatabase.OPEN_READWRITE );

        }
    }
    catch ( SQLException e ) {
        e.printStackTrace();
        Log.e( this.getClass().toString(), "Error while checking db" );
    }
    if ( checkDb != null ) {
        checkDb.close();
    }
    return checkDb != null;

}


public SQLiteDatabase openDataBase() throws SQLException {
    String path = DB_PATH + DB_NAME;
    if ( database == null ) {
        createDataBase();
        database = SQLiteDatabase.openDatabase( path, null, SQLiteDatabase.OPEN_READWRITE );

        try {
            database.setVersion(DB_VERSION);
        }catch (SQLiteDiskIOException dio){
            dio.printStackTrace();
            database.setVersion(DB_VERSION);
        }
    }
    return database;
}


public void onCreate( SQLiteDatabase db ) {
    Log.e( "Node ", "onCreate Database Deleting and Copying latest DB" );
}

DB helper methods

public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) {
    Log.e( "Node ", "onUpgrade Database Deleting and Copying latest DB" );
    if ( oldVersion < newVersion ) {
        Log.v( "Test", "Within onUpgrade. Old is: " + oldVersion + " New is: " + newVersion );
         //getFavorites( db) ;
        context.deleteDatabase( DB_NAME );
        try {
            copyDataBase();
             //setFavorites( db );
        }
        catch ( IOException e ) {
            e.printStackTrace();
        }

        // new BackupDBAsync(context).execute();
    }
}

This how we get the SQLiteHelper's instance

dbOpenHelper = new ExternalDbOpenHelper( getActivity(), AppConstants.DB_NAME );
    database = dbOpenHelper.openDataBase();
Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Sandy_4693
  • 128
  • 7
  • Hello Sandy_4693 I have exactly the same problem, my database code (mostly equal as yours) seems to be working well in all Android devices except on ZTE. I can not find any solution, did you find the solution to this? – Wonton Mar 25 '18 at 16:18
  • @Wonton no dude, didn't find any solution to it. – Sandy_4693 Apr 02 '18 at 15:54
  • Hello, have you found a solution for this? – observer Jul 31 '18 at 08:37
  • @observer no, and during last days this problem is happening too with Google Pixel. I'll try with Google support. – Wonton Aug 16 '18 at 14:22

1 Answers1

0

Try

db.close();

before

copyDataBase();
Alex
  • 1