gpt4 book ai didi

Android rawQuery 结果检查

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:10 26 4
gpt4 key购买 nike

我一遍又一遍地寻找这个问题的答案,但你是我唯一的机会。事情是这样的:

我有一个 DBAdapter 类、MakeListActivity 和 SeeListActivity。

在DBAdapter中我做了如下方法:

public Cursor commitQuery(String query)
{
return myDataBase.rawQuery(query,null);
}

MakeListActivity 有以下方法:

String query = "INSERT INTO list('name','description') VALUES('"+name+"','"+desc+"')";
dbAdapter.commitQuery(query);

最后,SeeListActivity 具有以下代码:

Cursor c = dbAdapter.commitQuery("SELECT * FROM list");
if(c!=null)
{
c.moveToFirst();
Log.i("cursor",c.getString(1));
}

我实际上不确定我是否正确插入了数据,但是当我想查看数据时(在我的例子中使用 logcat 记录)我得到“不幸的是,com.dstankovic.app 已停止。”

这里也是logcat错误:


02-26 00:31:11.762: E/AndroidRuntime(660): java.lang.IllegalStateException: Could not execute method of the activity
02-26 00:31:11.762: E/AndroidRuntime(660): at android.view.View$1.onClick(View.java:3599)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.view.View.performClick(View.java:4204)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.view.View$PerformClick.run(View.java:17355)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.os.Handler.handleCallback(Handler.java:725)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.os.Looper.loop(Looper.java:137)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-26 00:31:11.762: E/AndroidRuntime(660): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 00:31:11.762: E/AndroidRuntime(660): at java.lang.reflect.Method.invoke(Method.java:511)
02-26 00:31:11.762: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-26 00:31:11.762: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-26 00:31:11.762: E/AndroidRuntime(660): at dalvik.system.NativeStart.main(Native Method)
02-26 00:31:11.762: E/AndroidRuntime(660): Caused by: java.lang.reflect.InvocationTargetException
02-26 00:31:11.762: E/AndroidRuntime(660): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 00:31:11.762: E/AndroidRuntime(660): at java.lang.reflect.Method.invoke(Method.java:511)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.view.View$1.onClick(View.java:3594)
02-26 00:31:11.762: E/AndroidRuntime(660): ... 11 more
02-26 00:31:11.762: E/AndroidRuntime(660): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-26 00:31:11.762: E/AndroidRuntime(660): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
02-26 00:31:11.762: E/AndroidRuntime(660): at com.dulerock.eatwell.MakeNewListActivity.SaveList(MakeNewListActivity.java:278)

有人可以给我一些提示吗?提前致谢

最佳答案

这里游标不为空但是没有数据...你需要确定游标中有数据

moveToFirst()如果游标中有数据则返回true,如果为空则返回false

试试这个

Cursor c = dbAdapter.commitQuery("SELECT * FROM list");
if(c.moveToFirst())
{
Log.i("cursor",c.getString(1));
}

关于Android rawQuery 结果检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15078912/

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