Take the 2-minute tour ×

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

The error is

android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed: , while compiling: SELECT count(*) FROM words

The strangest in that I receive the error only on my device (lg p500), and the program works fine on the emulator.

My code related to the database:

dbHelper = new MySQLiteHelper(context);
    try {
    }catch(IOException ioe) 
        throw new Error("Unable to create database");
long tableSize = dbHelper.countTableRows(MySQLiteHelper.TABLE_WORDS); //The error appears

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;

public void onCreate(SQLiteDatabase database) {


public void createDataBase() throws IOException{         
    boolean dbExist = checkDataBase(); 
        //do nothing - database already exist
        //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.
        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)

    //Close the streams

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

public synchronized void close() {
    if(myDataBase != null)

public long countTableRows(String table)
    long rowsCount = DatabaseUtils.queryNumEntries(myDataBase, table);
    return rowsCount;

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.

I have checked that the database has android_metadata table with locale="ru_RU".

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...

share|improve this question
Tried to launch the program on other device (HTC Wildfire) with the same result. P.S. is it OK that my SQLite database words.db is 291 KB and my compiled APK is only 143 KB ? –  forcelain Feb 17 '12 at 8:47
add comment

2 Answers

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.

share|improve this answer
add comment

I think its your database version so can you change that database version as 3

share|improve this answer
Thanks for the answer. Unfortunately it hasn't helped. –  forcelain Feb 17 '12 at 5:40
add comment

Your Answer


