使用 Cursor 获取字段值

编程入门 行业动态 更新时间:2024-10-26 16:28:05
本文介绍了使用 Cursor 获取字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建一个应用程序,但我在使用 Cursor 时遇到了问题.我有一个 SQLiteDatabase,当我尝试使用此函数获取值时,它会返回一个 Cursor:

I'm creating an application and I have problems with Cursor. I have an SQLiteDatabase that returns me a Cursor when I try to fetch the values with this function:

public Cursor fetchOption(long rowId) throws SQLException { Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; }

我不知道如何获取Cursor中字段的值.如果我这样做:

I don't know how to obtain the value of the field in the Cursor. If I do that like so:

String a = mOptionDb.fetchOption(0).getColumnName(0).toString(); String b = mOptionDb.fetchOption(0).getColumnName(1).toString(); String c = mOptionDb.fetchOption(0).getColumnName(2).toString();

我只获取列的名称(_id, title, body)而不是值.有关如何实现这一目标的任何建议?

I only obtain the name of the columns (_id, title, body) but not the values. Any suggestions on how to achieve this?

推荐答案

我认为您可以忘记检查 null.

I think you can forget about checking for null.

改为检查是否有数据,然后使用游标访问列:

Instead check if there is data and then access the columns using the cursor:

Cursor cursor = fetchOption(0); if (cursor.moveToFirst()) // data? System.out.println(cursor.getString(cursor.getColumnIndex("title")); cursor.close(); // that's important too, otherwise you're gonna leak cursors

阅读 Android 教程也可能有意义.记事本教程似乎符合要求:developer.android/指南/教程/notepad/index.html

It might also make sense to read an Android tutorial. The notepad tutorial seems to fit the bill: developer.android/guide/tutorials/notepad/index.html

更多推荐

使用 Cursor 获取字段值

本文发布于:2023-07-09 14:30:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1086997.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   Cursor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!