jeudi 3 décembre 2015

SQLite Database not working correctly after new field added

Today I was working with SQLite on my android phone, everything was working accordingly until I added a new Field Constant.SUB_SIDEBARCOLORin my Constant.SUB_CATEGORY_TABLE table of my database, suddenly my queries starting failing and the working ones fetched wrong information, I would all the sudden have one item appear twice in my listview when before the field was added it was not the case, as soon as I added the new field I upgraded my database version so that onUpGrade would be called, it seemed it was never called, then I stated getting field not found SQLite errors, so I thought of deleting the database since even onUpgrade would not be called, I deleted the database by navigating to my /data/data/database/databaseName and deleted all the files in there even the jornals things seemed to work only for a minute then I realised my old data was been queried and found, I would call the categoryItemsNumber() method and it would return exactly the same row number as it was giving before I added the new field, below is my database helper:

public class UserDataHolderDB extends SQLiteOpenHelper {


public UserDataHolderDB(Context context) {


    super(context, Constant.DATABASE_NAME, null, Constant.DATABASE_VERSION);
}


public UserDataHolderDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
    super(context, name, factory, version, errorHandler);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_TABLE = " CREATE TABLE IF NOT EXISTS " + Constant.USER_DETAILS_TABLE +
            "(" + Constant.TABLE_ID + " INTEGER PRIMARY KEY," + Constant.USER_NAME + " TEXT," + Constant.USER_IMAGE_URI + " TEXT" + ")";

    db.execSQL(CREATE_TABLE);

    String CREATE_REF_IMG_TABLE = " CREATE TABLE IF NOT EXISTS " + Constant.REFERENCE_IMG_TABLE +
            "(" + Constant.REFERENCE_IMG_ID + " INTEGER PRIMARY KEY," + Constant.REFERENCE_IMG + " TEXT" + ")";

    db.execSQL(CREATE_REF_IMG_TABLE);

    String CREATE_CATEGORY_TABLE = " CREATE TABLE IF NOT EXISTS " + Constant.CATEGORY_TABLE +
            "(" + Constant.CATEGORY_TB_ID + " INTEGER PRIMARY KEY," + Constant.CATEGORY + " TEXT," + Constant.CATEGORY_DESC + " TEXT" + ")";

    db.execSQL(CREATE_CATEGORY_TABLE);

    String CREATE_CATEGORY_RES_TABLE = " CREATE TABLE IF NOT EXISTS " + Constant.CLOTHING_TABLE +
            "(" + Constant.CLOTHING_TB_ID + " INTEGER PRIMARY KEY," + Constant.FK_CATEGORY_ID + " TEXT," + Constant.CLOTHING_IMG_URI + " TEXT" + ")";

    db.execSQL(CREATE_CATEGORY_RES_TABLE);


    String CREATE_SUB_CATEGORY_TABLE = " CREATE TABLE IF NOT EXISTS " + Constant.SUB_CATEGORY_TABLE +
            "(" + Constant.SUB_CATEGORY_ID + " INTEGER PRIMARY KEY," + Constant.SUB_CATEGORY_FK  + " TEXT," + Constant.SUB_DESCRIPTION + " TEXT," + Constant.SUB_IMAGEPATH + " TEXT, " +Constant.SUB_SIDEBARCOLOR+ " TEXT"+")";

    db.execSQL(CREATE_SUB_CATEGORY_TABLE);


    String CREATE_ITEM_TABLE = " CREATE TABLE IF NOT EXISTS " + Constant.ITEM_TABLE +
            "(" + Constant.ITEM_ID + " INTEGER PRIMARY KEY," + Constant.ITEM_FK  + " TEXT," + Constant.ITEM_IMG_URI + " TEXT," + Constant.ITEM_DESCRIPTION + " TEXT" + ")";

    db.execSQL(CREATE_ITEM_TABLE);




}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    db.execSQL(" DROP TABLE IF EXISTS " + Constant.USER_DETAILS_TABLE + ";");

    db.execSQL(" DROP TABLE IF EXISTS " + Constant.REFERENCE_IMG_TABLE + ";");

    db.execSQL(" DROP TABLE IF EXISTS " + Constant.CATEGORY_TABLE + ";");

    db.execSQL(" DROP TABLE IF EXISTS " + Constant.CLOTHING_TABLE + ";");

    db.execSQL(" DROP TABLE IF EXISTS " + Constant.ITEM_TABLE + ";");

    onCreate(db);

}


public String categoryItemsNumber(int id) throws SQLException{


    System.err.println("started");
    int mCount = 0;


    String test = "SELECT * FROM "+Constant.ITEM_TABLE;

    String test2 = "SELECT * FROM "+Constant.ITEM_TABLE+" WHERE "+Constant.ITEM_FK
            +" = ? ";

    String sQuery = " SELECT " + Constant.SUB_CATEGORY_ID + ",  " + Constant.ITEM_IMG_URI +
            ", " + Constant.ITEM_DESCRIPTION +", "+Constant.SUB_SIDEBARCOLOR+" FROM  " + Constant.SUB_CATEGORY_TABLE +
            " INNER JOIN " + Constant.ITEM_TABLE + " ON " + Constant.SUB_CATEGORY_ID + " = " + Constant.ITEM_FK
            + " WHERE " + Constant.SUB_CATEGORY_ID + " = ?";



    String[] args = new String[1];
    args[0] = String.valueOf(id);


    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(sQuery, args);
    ///Cursor cursor = db.rawQuery(test, null);


    System.err.println("tensing up");
    // looping through all rows and adding to list
    System.err.println("mCount Before: "+mCount);

    System.err.println("Number of existing rows" + cursor.getCount());

    if (cursor.moveToFirst()) {
        do {
            mCount = cursor.getCount();

        } while (cursor.moveToNext());


    }
    System.err.println("mCount After: "+mCount);
    return String.valueOf(mCount);
}


 public void addNewUser(UserModel user) throws SQLException{

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Constant.USER_NAME, user.getName()); // Contact Name
    values.put(Constant.USER_IMAGE_URI, user.getImgUri()); // Contact Phone Number

    Log.d("DB_Working:", "name :" + user.getName());
    Log.d("DB_Working:", "imgUr :" + user.getImgUri());

    // Inserting Row
    db.insert(Constant.USER_DETAILS_TABLE, null, values);

    Log.d("DB_Done:", "name :" + user.getName());
    Log.d("DB_Done:", "imgUr :" + user.getImgUri());
    db.close();

}

}

To be honest I do not think anything is wrong with it, it was working correctly before the new field was added so I do not see why a new field would render it useless, it must be something to do with the database itself, if there is anyone who has faced a similar problem please help before i ruin my perfectly running app trying to change things that are actually working.

Aucun commentaire:

Enregistrer un commentaire