gpt4 book ai didi

android - 如何正确初始化游标?

转载 作者:行者123 更新时间:2023-12-02 13:33:50 25 4
gpt4 key购买 nike

我正在尝试调用数据库查询并访问数据。查询工作正常,并且光标在android device1中移动到了firs,但在android device2中没有。在device2中,它显示此错误

E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 7 columns.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.stock_design, PID: 25451
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stock_design/com.example.stock_design.Edit}: 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.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

这是数据库 Controller 类
val getEdit:List<Item_Edit> get(){
val db=context.openOrCreateDatabase(REAL_DATABASE, Context.MODE_PRIVATE, null)

val query ="With cte as (SELECT * FROM Transaction_table ORDER BY (CASE WHEN isbn = '$edt_isbn' AND location = '$edt_lc' AND branch = '$edt_br'THEN 2 ELSE 1 END), branch) SELECT T.isbn,T.branch,T.location,T.qty,M.article,M.description,M.price FROM cte T LEFT JOIN Master M WHERE T.isbn = M.isbn"

val cursor = db.rawQuery(query,null)
println(edt_isbn)
if (cursor.moveToFirst()) {

do {
seItem.add(Item_Edit(cursor.getString(cursor.getColumnIndex("branch")),
cursor.getString(cursor.getColumnIndex("location")),
cursor.getString(cursor.getColumnIndex("isbn")),
cursor.getString(cursor.getColumnIndex("article")),
cursor.getString(cursor.getColumnIndex("description")),
cursor.getString(cursor.getColumnIndex("price")),
cursor.getString(cursor.getColumnIndex("qty")) ))

} while(cursor.moveToNext())
}

else{
ck_edt = "false"
}
db.close()
return seItem
}

最佳答案

此错误仅在device2上发生吗?

如果是这样,则错误可能出在该设备的架构中。

failed to read row 0,column -1

表示您正在尝试从不存在的列中进行读取。

检查列名中是否有拼写错误。另外,列名区分大小写,因此也请检查。

关于android - 如何正确初始化游标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59871679/

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