【问题标题】:Error Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it [duplicate]错误 无法从 CursorWindow 读取第 0 行 col -1。确保在从光标访问数据之前正确初始化光标[重复]
【发布时间】:2020-03-04 03:33:36
【问题描述】:

我收到一个错误

无法从 CursorWindow 读取第 0 行 col -1。确保在从光标访问数据之前正确初始化光标。

我已经尝试了所有方法,但似乎没有任何效果

运行项目后,我的应用程序停止了这个方法:

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;
}

表创建

/_____________(我的桌子)_______________/ String Crete_Table_Product_For_Multi_Value = "创建表如果不存在 product_Multi_Value( Product_Type TEXT NOT NULL,product_date TEXT NOT NULL,supplier_date TIMESTAMP 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) REFERENCES Supplier(_id), FOREIGN KEY(_id) REFERENCES 产品(_id));";

错误日志

错误:

E/CursorWindow:无法从具有 1 行 1 列的 CursorWindow 读取第 0 行第 -1 列。
D/AndroidRuntime:关闭虚拟机
E/AndroidRuntime: 致命异常: main
进程:com.ans.accouting_s_p,PID:1349
java.lang.IllegalStateException: 无法为 android:onClick 执行方法

在 androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) 在 android.view.View.performClick(View.java:5623) 在 android.view.View$PerformClick.run(View.java:22433) 在 android.os.Handler.handleCallback(Handler.java:751) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6316) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 引起:java.lang.reflect.InvocationTargetException 在 java.lang.reflect.Method.invoke(本机方法) 在 androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 在 android.view.View.performClick(View.java:5623) 在 android.view.View$PerformClick.run(View.java:22433) 在 android.os.Handler.handleCallback(Handler.java:751) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6316) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 原因:java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行 col -1。确保在从光标访问数据之前正确初始化光标。 在 android.database.CursorWindow.nativeGetDouble(本机方法) 在 android.database.CursorWindow.getDouble(CursorWindow.java:543) 在 android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:87) 在 com.ans.accouting_s_p.databaseTable.DataSourceProduct.getpriceDataByNameProductAndSupplier(DataSourceProduct.java:322)

【问题讨论】:

标签: java android-sqlite


【解决方案1】:

问题是您试图访问游标中的列 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 ;```

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多