- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一遍又一遍地寻找这个问题的答案,但你是我唯一的机会。事情是这样的:
我有一个 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/
我们有这两个类 MainActivity 和一个 DBHelper DBHelper创建数据库和两个表一个表,masterPW ,只有一行两列,ID和 ColPW .一旦创建并插入数据,该表将仅包含一
我正在对我的数据库进行更新,但它不起作用。这是我的代码: list.setOnItemClickListener(new OnItemClickListener() { @Overri
我一遍又一遍地寻找这个问题的答案,但你是我唯一的机会。事情是这样的: 我有一个 DBAdapter 类、MakeListActivity 和 SeeListActivity。 在DBAdapter中我
我想实现 Android Room 持久性。 这是我的 DAO 界面。 @Dao interface FoodDao { /** * Returns all data in tabl
我得到了这个方法,它应该检查数据库中的给定数据并显示包含该数据的整行。 movies 是表的名称,year 是列的名称。 我已经尝试了几个小时,但找不到原因,为什么它会抛出此错误: 11-24 19:
因此,目前在添加到我的数据库时,我执行此 select 语句获取结果并将其添加到我的数据库中的单独列中,如下所示: public void addGasLog(GasLog gasLog){
一个简单的问题,我在互联网上找不到它,可能我看错了“术语”。但是如何制作带有“if contains”的SQL查询语句 "SELECT something FROM " + TABLE_NAME +
我的准备好的 SQL 语句有一个奇怪且无法解释的错误,它没有提供与 noprepared 语句相同的结果。 这两个版本不应该返回相同的结果吗? 准备好的版本(结果错误): sb.append(" WH
当我使用正常的 13 位条码运行以下任一查询时,我的数据库返回的结果非常好。但是,当我使用 8 位条形码运行完全相同的代码时,即使结果存在于数据库中,也不会在第二个查询中找到。对我来说,这些查询看起来
为了获得查询的多个结果,我使用了 Cursor c = mydatabase.rawQuery("command", null); 然后是 while (c.moveToNext()) { ... }
我希望能够使用原始查询按升序对单词进行排序(因为我将使用游标)。但是,当我尝试按字母顺序对单词进行排序时,我的应用程序崩溃了,出现了这个错误: android.database.sqlite.SQLi
我正在使用 String.format 将 SQLite 查询构建为 String 并将其传递给 SQLiteDatabase.rawQuery。我最后传递的整数参数被硬编码为零(我正在寻找大于零的任
如何执行下面的查询 select * from regions where field1 in('word1', 'word2') 使用 sqlite rawQuery 方法? 我试过下面的代码: S
我正在尝试使用带有 simpleCursorAdapter 的 rawQuery 来显示到 ListView ,但它一直向我抛出错误 04-29 13:36:36.453: ERROR/Android
什么都试过了。此更新不起作用。没有错误或其他什么 - 它只是不更新表格。 void SetDisable (Integer ID, boolean action){ Integ
我正在使用MySQLI Wrapper如 docs 中所述关于运行 rawQuery。由于它没有任何搜索数据库的功能,所以我自己编写了。下面是正在使用的代码。 $songs = $db->Object
Android 中 SQLite 数据库的以下 rawQuery 不返回任何结果: String query = "SELECT * FROM " + "(SELECT MATCH_ID, P
我正在运行以下命令,但以下列的列名称不明确 Cursor cursor = getWritableDatabase().rawQuery("select ((odometer) - y.odomete
我正在尝试在 WHERE 中使用动态创建的列表,例如: select * from books where id in (123, 120, 125) and bookCover = 'Hardcov
我创建了这个方法来了解我的 Sqlite 数据库中是否存在联系人。 public Cursor doesContactExist(String name) { return db.rawQue
我是一名优秀的程序员,十分优秀!