任何人都能解释一下这是什么问题吗? 我开发了使用自己的数据库的应用程序。一切正常,但有一天我开始收到错误
long rowsCount = DatabaseUtils.queryNumEntries(myDataBase, table);
错误是
android.database.sqlite.SQLiteDatabaseCorruptException:数据库磁盘映像格式错误:,同时编译:SELECT count(*)FROM words
最奇怪的是我只在我的设备(lg p500)上收到错误,程序在模拟器上运行正常。
我的代码与数据库有关:
dbHelper = new MySQLiteHelper(context);
try {
dbHelper.createDataBase();
}catch(IOException ioe)
{
throw new Error("Unable to create database");
}
...
dbHelper.openDataBase();
long tableSize = dbHelper.countTableRows(MySQLiteHelper.TABLE_WORDS); //The error appears
...
MySQLiteHelper代码:
public class MySQLiteHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "words.db";
private static final int DATABASE_VERSION = 1;
private static final String DB_PATH = "/data/data/ru.my.program/databases";
public static final String TABLE_WORDS = "words";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_WORD = "word";
private final Context myContext;
public SQLiteDatabase myDataBase;
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
myContext = context;
}
@Override
public void onCreate(SQLiteDatabase database) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
this.close();
try
{
copyDataBase();
}
catch(IOException e)
{
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
File dbFile = new File(DB_PATH + "/" + DATABASE_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myinput = myContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outfilename = DB_PATH + "/" +DATABASE_NAME;
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream("/data/data/ru.my.program/databases/words.db");
// transfer byte to inputfile to 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();
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + "/" +DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READONLY);
Log.i(MySQLiteHelper.class.getName(), "Open DB");
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
public long countTableRows(String table)
{
long rowsCount = DatabaseUtils.queryNumEntries(myDataBase, table);
return rowsCount;
}
我已经使用DDMS检查了设备上的数据库,并确信它被复制到适当的位置,并且与资产一样。
我检查过数据库中有android_metadata表,其中locale =“ru_RU”。
我已经检查过“words”表中的“_id”列。
我尝试手动删除应用程序文件夹(使用数据库),但它没有帮助。
我甚至尝试重命名我的应用程序包......
答案 0 :(得分:0)
我认为它是您的数据库版本,因此您可以将该数据库版本更改为3
答案 1 :(得分:0)
重新创建数据库文件后问题已消失。 似乎数据库文件应该还有一个表“sqlite_sequence”。该表应包含两列:name和seq。 “name”表示您创建的表名(不包括android_metadata),“seq”表示这些表中的行数。