Android 游标列

标签 android sqlite

可以安全地假设 Android 游标仅包含我请求的列,并以与它们在 select 语句中相同的顺序返回它们,即。 e.下面的代码会一直有效吗?

Cursor cursor = db.query("NAMES",
    new String[] {"FIRSTNAME", "LASTNAME", "MOBILE"},
    "ID = " + requestedId, null, null, null, null, null);

if (cursor.moveToFirst()) {
    firstname = cursor.getString(0);
    lastname = cursor.getString(1);
    mobile = cursor.getString(2);
}

或者我应该坚持像下面那样显式获取列索引,因为 Android SQLite 可能会在 query() 返回的游标中添加一些辅助数据或更改列的顺序?

firstname = cursor.getString(cursor.getColumnIndex("FIRSTNAME"));
lastname = cursor.getString(cursor.getColumnIndex("LASTNAME"));
mobile = cursor.getString(cursor.getColumnIndex("MOBILE"));

最佳答案

Is is safe to assume that Android cursor only has the columns I requested and returns them in the same order as they are in the select statement, i. e. will the following code always work?

是的,它很安全。

在幕后,它只是 SQLiteQueryBuilder 进行愚蠢的字符串连接。您可以检查 source了解更多。

关于Android 游标列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28514035/

相关文章:

android - SQLite 没有空构造函数

android - Firebase 而不是 SQLite

Android ICS 小部件(开关)和旧版本

Android Studio 错误 : build scan was not published

ios - NSMutableDictionary 给出所有相同的值而不是新的新值

swift - 使用预填充的核心数据部署应用程序

java - 使用 onOptionsItemSelected 从带有 PreferenceFragments 的 PreferenceActivity 上升

android - GSON : Custom serialization for String

android - 带有动画和填充的可扩展 ListView 集 selectionfromTop

java - 重新启动应用程序后 SQLite DB 被删除