Question

I am working on a project that containg tables in SQL and shows them in ListViews, i get "column '_id' does not exist" crush when i try to get to the activity containing the listview, i have checked other answers here and they all say i have to have a colum named "_id", and i do, what else could cause this error?

this is my constants class

final static String DB_CLIENTS_ID = "_id";
    final static String DB_CLIENTS_NAME = "name";
    final static String DB_CLIENTS_BALANCE = "balance";
    final static String DB_CLIENTS_IDNUM = "idNum";
    final static String DB_CLIENTS_TYPE = "type";

here is the hendler function that gets the curser:

    public Cursor queryClients(){

                db = dbOpenHelper.getReadableDatabase();


                Cursor cursor = db.query(dbConstants.TABLE_NAME_CLIENTS,
                            null, null, null, null, null,
                            dbConstants.DB_CLIENTS_NAME+ " ASC");           

                return cursor;
            }

here is the snippet that uses the curser to make the listview:

dbhendler = new dbHendler(this);
        Cursor c = dbhendler.queryClients();
        startManagingCursor(c);

        String[] from = new String[] {dbConstants.DB_CLIENTS_ID, dbConstants.DB_CLIENTS_NAME,dbConstants.DB_CLIENTS_BALANCE};

        int[] to = new int[] {R.id.account_list_id_number, R.id.account_list_client_name, R.id.account_list_balance};


        adapter = new SimpleCursorAdapter(this, R.layout.account_list_line, c, from, to);

ListView lv = (ListView) findViewById(R.id.listView1);

        lv.setAdapter(adapter);

what could be the problem besides having no colum named "_id"?

edit:

here is the log cat:

01-28 10:00:31.806: E/AndroidRuntime(27937): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ziv.bank_app/com.ziv.bank_app.ClientListActivity}: java.lang.IllegalArgumentException: column '_id' does not exist

edit:

code for creating table:

public void onCreate(SQLiteDatabase db) {

        String sql1 = ""
                + "CREATE TABLE "+ dbConstants.TABLE_NAME_CLIENTS+ " ("
                +dbConstants.DB_CLIENTS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                +dbConstants.DB_CLIENTS_NAME + " TEXT,"
                +dbConstants.DB_CLIENTS_IDNUM + " INTEGER,"
                +dbConstants.DB_CLIENTS_TYPE + " TEXT,"
                +dbConstants.DB_CLIENTS_BALANCE + " REAL"
                + ")";

        db.execSQL(sql1);
Was it helpful?

Solution

when first seing the problem i saw here that the answer was to have a string named "_id", i changed it in my table creation file, however a new file was never created, it would have been created on a new device/emulator but on mine it still used the one i have created.

create a new database file by simply changing its name in the table creation code, and the problem is solved.

edit:

also raising the version number would do the trick

OTHER TIPS

I had similar problem this is the way I tried it out

You can give it a try :

1) first delete your database from your device or emulator

emulator : within data/data/databases

device :use adb shell and run-as command

2) create a new database with different name(not same as previous one).

Let me know if problem persists

Hope It could help you ...

If i am not wrong then try out this in your queryClients():

Cursor cursor = db.query(dbConstants.TABLE_NAME_CLIENTS, new String[] {"_id","name","balance","idNum","type"}, 
null, null, null, null, "name ASC ");

or Try this:

Cursor mCursor = db.query(true, dbConstants.TABLE_NAME_CLIENTS,
            new String[] {"_id","name","balance","idNum","type"}, 
null,null, null, null, null, null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top