0

I am using a SQLite database and I got this this error:
Caused by: android.database.sqlite.SQLiteException: no such column: coin (code 1): , while compiling: SELECT _id, name, age, coin, log FROM person_tb1 WHERE name = ?

I have no idea why it show me this!
I have tried it before its working fine!

Here is my code:

public ArrayList<Person> getPersons (){

    persons.clear();

    SQLiteDatabase dba = this.getReadableDatabase();

    Cursor cursor = dba.query(Constants.TABLE_NAME,
            new String[]{Constants.KEY_ID, Constants.NAME, Constants.AGE , Constants.COIN , Constants.LOG},null,null,null,null,null);

    if(cursor.moveToFirst()){
        do {
            Person p = new Person();
            p.setName(cursor.getString(cursor.getColumnIndex(Constants.NAME)));
            p.setAge(cursor.getInt(cursor.getColumnIndex(Constants.AGE)));
            p.setCoin(cursor.getInt(cursor.getColumnIndex(Constants.COIN)));
            p.setLog(cursor.getInt(cursor.getColumnIndex(Constants.LOG)));
            p.setPersonId(cursor.getInt(cursor.getColumnIndex(Constants.KEY_ID)));

            persons.add(p);

        }while (cursor.moveToNext());

        cursor.close();
        dba.close();
    }
    return persons;
}

and here the select method :

 public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "(" +
            Constants.KEY_ID + " INTEGER PRIMARY KEY, " + Constants.NAME + " TEXT, "+
            Constants.AGE + " INT, " + Constants.COIN + " INT, " + Constants.LOG + " INT);";

    db.execSQL(CREATE_TABLE);
}

// EDIT : (OnUpgrade)

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + Constants.TABLE_NAME);

    onCreate(db);
}
0

1 Answer 1

3

Wild guess: You added the column after running the app once.
If so (i have tried it before its working fine !), just unistall and reinstall your app.

OR you can simply increment your DATABASE_VERSION constant.

[EDIT]

But the second method won't work, since your current onUpgrade() method is buggy.

db.execSQL("DROP TABLE IF EXISTS" + Constants.TABLE_NAME);

Won't delete the table. And so, it won't be recreated, even.
You need to insert a space before the table name:

db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_NAME);
Sign up to request clarification or add additional context in comments.

6 Comments

or just +1 the db version, and make sure you apply whatever migration is necessary in the onUpgrade (which usually is just a drop/create)
Yes, another way to do that :)
i have the onUpgrade in my code ^^ ! look i updated my post
i have the onUpgrade in my code It's simply not enough to have it. You must activate it by incrementing your DATABASE_VERSION constant. OR, uninstall and reinstall your app. That's how it works. ALSO, your onUpgrade contains a bug, which prevents its work: you miss a space before the table name.
thank youu so much !! u saved my life xD ! i just reinstall my app !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.