0

I am working on an application in which I am creating my local sqlite database, and its working successfully in all the devices except the Google Nexus One with OS 2.3.3. Logs:

 CREATE TABLE android_metadata failed
Failed to setLocale() when constructing, closing the database
android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed
    at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
    at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1987)
    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1855)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:567)
    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
    at com.weg.ecatalogue.database.ECatalogueDatabase.open(ECatalogueDatabase.java:76)
    at com.weg.ecatalogue.HomeScreen.onCreate(HomeScreen.java:219)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3839)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)
  Deleting and re-creating corrupt database /data/data/com.weg.ecatalogue/databases/ECatalogue
  android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed
    at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
    at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1987)
    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1855)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:567)
    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
    at com.weg.ecatalogue.database.ECatalogueDatabase.open(ECatalogueDatabase.java:76)
    at com.weg.ecatalogue.HomeScreen.onCreate(HomeScreen.java:219)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3839)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)

Please suggest me, how can I solve this problem..!!:(

public class ECatalogueDatabase {

    private static final String DB_PATH = "/data/data/com.weg.ecatalogue/databases/";
    public static final String DATABASE_NAME = "ECatalogue";
    public static final String DATABASE_TABLE = "T_Electrical";
    public static final int DATABASE_VERSION = 1;

    public static final String KEY_ROWID="id";
    public static final String KEY_PRODUCT_LINE="productline";

    public static final String KEY_VOLTAGE="voltage";
    public static final String KEY_OUTPUTHP="outputhp";

    public static final String KEY_FRAME="frame";
    public static final String KEY_RPM="rpm";

    private Context context=null;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    /**
     * Database creation sql statement
     */
    private static final String CREAT_DATABASE="Create Table if not exists "+ DATABASE_TABLE+"("+ KEY_ROWID +" INTEGER PRIMARY KEY NOT NULL,"
    +KEY_PRODUCT_LINE +" nvarchar ,"+ KEY_OUTPUTHP+" numeric ,"+ KEY_RPM +" nvarchar ,"+KEY_VOLTAGE +" nvarchar ," +KEY_FRAME +" nvarchar"+")";

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     *
     * @param ctx the Context within which to work
     */
    public ECatalogueDatabase(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
    //Helper class
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREAT_DATABASE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }

    }

    public ECatalogueDatabase open() //throws SQLException
    {
        try
        {
            db=DBHelper.getWritableDatabase();

        }catch(Exception exception)
        {
            exception.printStackTrace();
        }
        return null;
    }


    public void close()
    {
        DBHelper.close();
    }



    /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    public void createDataBase() throws IOException{

        @SuppressWarnings("unused")
        boolean dbExist = checkDataBase();

        /**
        * CHANGES DONE BY SHAILESH SHARMA TO SOLVE THE PROBLEM OF 2.2 HTC DESIRE IN CLIENT'S WIFE DEVICE :(
        */
        SQLiteDatabase db_Read = null;
        if(dbExist){
        //DO NOTHING IN THIS CASE
        }else{

        db_Read = DBHelper.getReadableDatabase();
        db_Read.close();
        }
        //=================================

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }

    }
    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase(){
        try{
            String myPath = DB_PATH + DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e){
            System.out.println(e+"sos sos sos sos");
        }

        if(db != null){

            db.close();

        }

        return db != null ? true : false;
    }
    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{


        InputStream myInput = context.getAssets().open(DATABASE_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DATABASE_NAME;


        OutputStream myOutput = new FileOutputStream(outFileName);


        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public int getDatabaseCount(){
        int count = 0;
        Cursor cursor = db.rawQuery("Select * from " + DATABASE_TABLE, null);
        if(cursor!=null){
            count = cursor.getCount();
        }
        cursor.deactivate();
        cursor.close();
        return count;
    }
Namphibian
  • 12,046
  • 7
  • 46
  • 76
Sam-In-TechValens
  • 2,501
  • 4
  • 34
  • 67

1 Answers1

0

If this particular database file works everywhere except in old Android versions, then it is likely that you are using some SQLite feature that is not available in older SQLite version.

For example, WAL is supported beginning with SQLite 3.7, but Froyo has SQLite 3.6.22.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • my mozilla firefox SQLite version is 0.7.7, is it creating any problem?? – Sam-In-TechValens Oct 10 '12 at 10:53
  • Execute `SELECT sqlite_version();` to get the version of the DB engine. Also, please show all `PRAGMA` commands that you execute in your database before shipping it. – CL. Oct 10 '12 at 11:15
  • Hello CL, my Sqlit version is 3.7.12.1 , but I am not using any PRAGMA command as you can see in my code.:( – Sam-In-TechValens Oct 10 '12 at 12:07
  • public int getDatabaseCount(){ int count = 0; Cursor cursor = db.rawQuery("Select * from " + DATABASE_TABLE, null); if(cursor!=null){ count = cursor.getCount(); } cursor.deactivate(); cursor.close(); return count; } I get cursor count 0 in nexus device but it must be like 100 based on the number of rows in my DB – Sam-In-TechValens Oct 10 '12 at 12:31
  • Your `copyDataBase` function (called from `createDataBase`) copies the DB file from the assets folder. Please show any `PRAGMA`s that you executed on that file. (Or try recreating it without any.) – CL. Oct 10 '12 at 12:42
  • ohk...CL, I am creating this DB using SQLite plugin of Firefox, I just used insert statements to insert data rows. Around 2000 insert statement were executed at a time, to create this DB. For example: insert into T_Electrical (id, productline, voltage, outputhp, frame, rpm) values ("1","TEFC - W22 NEMA Premium Efficiency","208-230/460V","1","143T","3600 rpm") – Sam-In-TechValens Oct 10 '12 at 12:48
  • And did you change any DB settings? – CL. Oct 10 '12 at 13:43
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17822/discussion-between-cl-and-samintechvalens) – CL. Oct 10 '12 at 14:55
  • Thanks alot CL....!!! Its was really very important for me....and soon let you know if get success..!! – Sam-In-TechValens Oct 11 '12 at 13:12