java - 错误 无法从 CursorWindow 读取第 0 行,第 -1 列。在访问数据之前确保游标已正确初始化

标签 java android-sqlite

我收到错误

Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

我已经尝试了一切,但似乎没有任何效果

运行项目后,我的应用程序在此方法上停止:

getPriceDataByNameProductAndSupplier

代码:

/*_____________________________ My query _______________________*/
public double getPriceDataByNameProductAndSupplier(int product_id,int supplier_id)
{

Product product=null;
double  Price_for_every_one=0.0;
String query= "select max(price_for_every_one) from product_Multi_Value where _id ="+ product_id +" and _idSupplier="+ supplier_id ;
Cursor cursor = sqLiteDatabase.rawQuery(query,null/*ew String[] {String.valueOf(product_id),
        String.valueOf(supplier_id)}*/); /*("product_Multi_Value",
                                     new String[] {"MAX(price_for_every_one) AS MAX"},"_id=? and _idSupplier=?",
                                     new String[] {String.valueOf(product_id),String.valueOf(supplier_id)},
                                     null,null,null)*/;// cursor.moveToFirst();
if (cursor !=null)
{
    cursor.moveToFirst();
    do
    {
        Price_for_every_one = cursor.getDouble(cursor.getColumnIndexOrThrow("price_for_every_one") );
        product = new Product(Price_for_every_one);
    }
    while (cursor.moveToNext());
}
return Price_for_every_one;
}

表创建

/_______________________(我的 table )_______________________/ String Crete_Table_Product_For_Multi_Value = "如果不存在则创建表product_Multi_Value( 产品类型文本不为空,产品日期文本不为空,供应商日期时间戳 DEFAULT CURRENT_TIMESTAMP,exp_date TEXT NOT NULL,quantity_package REAL NOT NULL, number_quantity_ber_package INTEGER NOT NULL,total_of_quantity REAL NOT NULL, Price_for_Allquantity REAL NOT NULL,price_for_every_one REAL NOT NULL, Price_for_sales REAL、_id INTEGER NOT NULL、idSupplier INTEGER NOT NULL、FOREIGN KEY( _idSupplier) 引用供应商 (_id),外键 (_id) 引用 产品(_id));";

错误日志:

错误:

E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ans.accouting_s_p, PID: 1349
java.lang.IllegalStateException: Could not execute method for android:onClick

at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:5623) at android.view.View$PerformClick.run(View.java:22433) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6316) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:5623)  at android.view.View$PerformClick.run(View.java:22433)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6316)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)  Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetDouble(Native Method) at android.database.CursorWindow.getDouble(CursorWindow.java:543) at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:87) at com.ans.accouting_s_p.databaseTable.DataSourceProduct.getpriceDataByNameProductAndSupplier(DataSourceProduct.java:322)

最佳答案

问题是您正在尝试访问游标中的 price_for_every_one 列,但它不存在,因为当您使用 MAX、MIN、SUM 等聚合方法时 它更改了结果中列的名称,除非您使用 AS 关键字

在 SQL 查询中

String query = "select max(price_for_every_one) from product_Multi_Value where _id ="+ product_id +" and _idSupplier="+ supplier_id ;

结果中的列名称为

max(price_for_every_one)

不是

price_for_every_one

所以你不能使用列名

Price_for_every_one = cursor.getDouble(cursor.getColumnIndexOrThrow("price_for_every_one") );

由于您仅选择一列,因此您可以通过索引访问该列

cursor.getDouble(0)

或者您可以在查询中使用 AS 关键字,如下所示

String query = "select max(price_for_every_one) As price_for_every_one from product_Multi_Value where _id ="+ product_id +" and _idSupplier="+ supplier_id ;```

希望对你有帮助

关于java - 错误 无法从 CursorWindow 读取第 0 行,第 -1 列。在访问数据之前确保游标已正确初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60518674/

相关文章:

java - Ref to map vs. map to refs vs. multiple refs

java - 如果我们保持空闲一段时间,连接到 Azure MSSQL DB 时,Spring Boot 应用程序中的数据库连接会关闭

java - 如何阻止 Eclipse 卡在很长的自动完成列表上?

java - 如何更改 JComboBox 颜色?

java - 从数据库检索数据?

android - onDelete = NO_ACTION 导致错误 : "SQLiteConstraintException: FOREIGN KEY constraint failed" (code 787)

Android Live数据观察者异常

Android SQL查询多个WHERE参数

java - XSD - 验证该元素引用 XML 文件中的其他元素

java - Android APP 在 getWritableDatabase() 或 getReadableDatabase() 上崩溃