android - 未在特定设备品牌 ZTE-Z982 N9560 上获取 SQLite 表

标签 android sqliteopenhelper

我们收到“找不到这样的表”的 SQLite 异常。此问题仅在品牌为 ZTE(Z982 和 N95630)的设备上报告。在所有其他设备上,数据库工作正常,因此无法重现。数据库路径有问题吗?

下面是我们正在使用的 SQLite Helper 类

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();
}

创建数据库的方法

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" );
        }
    }
}

数据库是否存在

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" );
}

数据库辅助方法

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();
    }
}

这就是我们获取 SQLiteHelper 实例的方式

dbOpenHelper = new ExternalDbOpenHelper( getActivity(), AppConstants.DB_NAME );
    database = dbOpenHelper.openDataBase();

最佳答案

尝试

db.close();

之前

copyDataBase();

关于android - 未在特定设备品牌 ZTE-Z982 N9560 上获取 SQLite 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47811513/

相关文章:

android - 当收到来自 AdMob 的广告时,TableRow 中的 TextView 消失

java - 我们如何传递数据?

android - 如何根据sql数据库中的名称查询单个项目并返回游标?

android - 在不同的 SQLiteOpenHelper 实例中使用相同的 SQLiteDatabase 实例

android - 为每隔几秒发出一次请求的android应用程序创建服务器端

android - setSuggestionAdapter cursor - 需要更新它

android - SQLiteOpenHelper 单连接与多连接

android - libGDX/安卓 : How to loop background music without the dreaded gap?

android - 如何在 Android 运行时导入共享对象库?