我们得到了#34的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();
答案 0 :(得分:0)
尝试
db.close();
之前
copyDataBase();