gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:06:33 47 4
gpt4 key购买 nike

这是我创建表格的方法...

"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 );"

如果我这样做。

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

然后它显示_ID,但是当我这样做时:

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

它给了我这个错误:

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)

最佳答案

Cursor 列名称区分大小写,并且 CursorAdapter 期望看到小写的 _id。 (SQL 标识符本身不区分大小写。)

重命名该列并重新创建表,或者仅以小写形式显式选择它,例如选择_id,...

关于java - Android SQLite java.lang.IllegalArgumentException : column '_id' does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35996611/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com