vendredi 29 mai 2015

SQLite can not find new column

I had made SQLite database to my android application and it worked fine until i tried to add new column called "state". I have read previous posts in forum, and I tried adding new database version, modifying onUpgrade method, i have also tried deleting app, cleaning cache, and adding ALTER table but i still get error: table has no collumn named state. So how do I update my database to make it work?

Heres my code

public class DatabaseHelper extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 3;

    // Database Name
    private static final String DATABASE_NAME = "dbase";

    // Contacts table name
    private static final String TABELL_BRUKERE = "bTab";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAVN = "navn";
    private static final String KEY_ADR = "adresse";
    private static final String KEY_BIL = "bil_kjennemerke";
    private static final String KEY_STATE = "state";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "DROP TABLE IF EXISTS " + TABELL_BRUKERE + " CREATE TABLE " + TABELL_BRUKERE + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAVN + " TEXT,"
                + KEY_ADR + " TEXT,"
                + KEY_BIL +  " TEXT"
                + KEY_STATE + " TEXT" + ")";

        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String CREATE_CONTACTS_TABLE = "DROP TABLE IF EXISTS " + TABELL_BRUKERE + " CREATE TABLE " + TABELL_BRUKERE + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAVN + " TEXT,"
                + KEY_ADR + " TEXT,"
                + KEY_BIL +  " TEXT"
                + KEY_STATE + " TEXT" + ")";
        if (oldVersion < 4 )
            db.execSQL(CREATE_CONTACTS_TABLE);
        // Create tables again
        onCreate(db);
    }
    public void addContact(Bruker bruker) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAVN, bruker.getNavn()); // navn
        values.put(KEY_ADR, bruker.getAdresse()); //  Adresse
        values.put(KEY_BIL, bruker.getBilmerke()); //  Bil
        values.put(KEY_STATE, bruker.getState()); //  state
        // Inserting Row
        db.insert(TABELL_BRUKERE, null, values);
        db.close(); // Closing database connection
    }


    public int getCount() {
        String countQuery = "SELECT  * FROM " + TABELL_BRUKERE;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }
    // Getting single contact
    public Bruker getContact(int id) {
            SQLiteDatabase db = this.getReadableDatabase();

            Cursor cursor = db.query(TABELL_BRUKERE, new String[] { KEY_ID,
                            KEY_NAVN, KEY_ADR,KEY_BIL,KEY_STATE }, KEY_ID + "=?",
                    new String[] { String.valueOf(id) }, null, null, null, null);
            if (cursor != null)
                cursor.moveToFirst();

            Bruker bruker = new Bruker(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3),cursor.getString(4));
            // return contact
            return bruker;
    }

    // Getting All Contacts
    public List<Bruker> getBrukere() {
        List<Bruker> contactList = new ArrayList<Bruker>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABELL_BRUKERE;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Bruker bruker = new Bruker();
                bruker.setID(Integer.parseInt(cursor.getString(0)));
                bruker.setNavn(cursor.getString(1));
                bruker.setAdresse(cursor.getString(2));
                bruker.setBilmerke(cursor.getString(3));
                bruker.setState(cursor.getString(4));

                // Adding contact to list
                contactList.add(bruker);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

    // Deleting single contact
    public void deleteContact(Bruker bruker) {}
}

Aucun commentaire:

Enregistrer un commentaire