问题描述
任何人能解释什么问题?
我开发我的应用程序,它使用我自己的数据库。所有工作正常,但有一天我开始接到在
Can anyone explain what's the problem?I developed my application which uses my own database. All worked fine, but in one day I began to receive the error at
long rowsCount = DatabaseUtils.queryNumEntries(myDataBase, table);
该错误是
android.database.sqlite.SQLiteDatabaseCorruptException:数据库磁盘映像格式不正确:,在编译:SELECT COUNT(*)FROM字
在最奇怪我收到的错误只在我的设备(LG P500),以及程序工作正常,在模拟器上。
The strangest in that I receive the error only on my device (lg p500), and the program works fine on the emulator.
我的code与数据库相关的:
My code related to the database:
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 code:
MySQLiteHelper code:
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设备上,并相信它是在一个适当的位置复制它,因为资产。
I have checked up the database on the device with the DDMS and was convinced that it is copied in a proper place and it same, as in assets.
我已经检查数据库具有android_metadata表区域=ru_RU。
I have checked that the database has android_metadata table with locale="ru_RU".
我已签,我在字表有列_id。
I have checked that I have the column "_id" in the "words" table.
我试图手动删除应用程序文件夹(与数据库),但它并没有帮助。
I tried to remove the application folder (with the database) manually, but it hasn't helped.
我甚至尝试重新命名我的应用程序包...
I even tried to rename my application package...
推荐答案
我已经重新创建数据库文件后问题已经消失。
看来数据库文件应该有一个更表sqlite_sequence。该表应该包含两列:名称和序列。 名为这些表中的行量表的名称,已创建(不含android_metadata),序列。
The problem has disappeared after I have recreated the database file.It seems that the database file should have one more table "sqlite_sequence". The table should contain two columns: name and seq. "name" for names of tables, which you have created (excluding android_metadata), "seq" for amount of rows in these tables.
这篇关于无法打开Android设备上我的数据库,但可以在模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!