Danyal Ahmed Chaudhry
Danyal Ahmed Chaudhry

Reputation: 93

Android SQLite java.lang.IllegalArgumentException: column '_id' does not exist

here is how i create the table ...

"CREATE TABLE IF NOT EXISTS Product (
    _ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , 
    Name VARCHAR NOT NULL, 
    Code VARCHAR NOT NULL, 
    Quantity VARCHAR NOT NULL, 
    PRICE DOUBLE NOT NULL );"

if i do this.

myDb = new DbAdapter(getApplicationContext());
        Cursor res = myDb.getData(tbs.productsTable, tbs.getProductsColumns(), " ");    
        toast(res.getColumnName(0));

then it shows _ID, but when i do this:

ListView lvItems = (ListView) findViewById(R.id.listViewProduct);
        Populate_Products todoAdapter = new Populate_Products(this, res);
        lvItems.setAdapter(todoAdapter);

it gives me this error:

03-14 19:36:52.459 31990-31990/com.example.danyalahmed.stockmanagement E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.danyalahmed.stockmanagement, PID: 31990 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.danyalahmed.stockmanagement/com.example.danyalahmed.stockmanagement.Activities.ListProducts}: java.lang.IllegalArgumentException: column '_id' does not exist at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) at android.app.ActivityThread.access$800(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5373) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) Caused by: java.lang.IllegalArgumentException: column '_id' does not exist at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303) at android.widget.CursorAdapter.init(CursorAdapter.java:172) at android.widget.CursorAdapter.(CursorAdapter.java:149) at com.example.danyalahmed.stockmanagement.Classes.Populate_Products.(Populate_Products.java:20) at com.example.danyalahmed.stockmanagement.Activities.ListProducts.loadData(ListProducts.java:52) at com.example.danyalahmed.stockmanagement.Activities.ListProducts.onCreate(ListProducts.java:37) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)  at android.app.ActivityThread.access$800(ActivityThread.java:156)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:211)  at android.app.ActivityThread.main(ActivityThread.java:5373)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

Upvotes: 0

Views: 1518

Answers (2)

Ashu
Ashu

Reputation: 98

Try _id instead of _ID in CREATE statement-

"CREATE TABLE IF NOT EXISTS Product (
_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , 
Name VARCHAR NOT NULL, 
Code VARCHAR NOT NULL, 
Quantity VARCHAR NOT NULL, 
PRICE DOUBLE NOT NULL );"

Upvotes: 1

laalto
laalto

Reputation: 152807

Cursor column names are case sensitive and CursorAdapter expects to see lowercase _id. (SQL identifiers themselves are not case sensitive.)

Either rename the column and recreate your table, or just select it explicitly in its lowercase form, e.g. SELECT _id, ....

Upvotes: 3

Related Questions