I'm using cursors to retrieve data from a database. I know how to retrieve entire columns to use on listviews and such, but I'm struggling to find a way to retrieve a single value.
我正在使用游标从数据库中检索数据。我知道如何检索整个列以用于listviews等,但我很难找到一种方法来检索单个值。
Let's say I have a table with two columns ("_id" and "Name") and I have ten records (rows) in that table. How would I get, for example, the Name in the third row? Considering I defined a cursor that reads that table:
假设我有一个包含两列(“_id”和“Name”)的表,我在该表中有十个记录(行)。例如,我如何得到第三行中的名字?考虑到我定义了一个读取该表的游标:
public Cursor getMyNameInfo() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String sqlTables = "MyNames";
qb.setTables(sqlTables);
Cursor c = qb.query(db, null, null, null,
null, null, null);
c.moveToFirst();
return c;
}
4 个解决方案
#1
7
Instead of c.moveToFirst()
use c.moveToPosition(2)
(Cursor
indexes are zero-based hence '2' is the third record).
而不是c.moveToFirst()使用c.moveToPosition(2)(游标索引是从零开始的,因此'2'是第三个记录)。
Remember to check that the Cursor
has valid data first though.
请记住先检查Cursor是否有有效数据。
EDIT:
编辑:
Once you've moved the cursor to the 3rd record as I explain above, use the following to just get the value of the "Name" column.
按照上面的说明将光标移动到第3条记录后,使用以下命令获取“Name”列的值。
String theName = c.getString(getColumnIndex("Name"));
#2
1
Cursor cursor = dbHelper.getdata();
System.out.println("colo" + cursor.getColumnCount() + ""
+ cursor.getCount());
cursor.moveToFirst();
if (cursor != null) {
while (cursor.isAfterLast() == false) {
String chktitle = title.trim().toString();
String str = cursor.getString(cursor.getColumnIndex("title"));
System.out.println("title :: "
+ cursor.getString(cursor.getColumnIndex("title")));
System.out.println("date :: "
+ cursor.getString(cursor.getColumnIndex("date")));
System.out.println("desc :: "
+ cursor.getString(cursor.getColumnIndex("desc")));
if (chktitle.equals(str) == true) {
tvAddfavorite.setText("Remove Favorite");
break;
}
cursor.moveToNext();
}
}
cursor.close();
#3
1
Add a WHERE
clause:
添加WHERE子句:
qb.appendWhere("_id = 2");
#4
0
Thanks to the answers above I created the following method to give the value of an item from another column but in the same row.
感谢上面的答案,我创建了以下方法,以便从另一列但同一行中提供项目的值。
public String getValueFromColumn(int position, String tableName, String columnToGetValueFrom) {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(tableName);
Cursor c = qb.query(db, null, null, null,
null, null, null);
c.moveToPosition(position);
String neededValue = c.getString(c.getColumnIndex(columnToGetValueFrom));
return neededValue;
}
Hope it helps anyone.
希望它能帮助任何人。
#1
7
Instead of c.moveToFirst()
use c.moveToPosition(2)
(Cursor
indexes are zero-based hence '2' is the third record).
而不是c.moveToFirst()使用c.moveToPosition(2)(游标索引是从零开始的,因此'2'是第三个记录)。
Remember to check that the Cursor
has valid data first though.
请记住先检查Cursor是否有有效数据。
EDIT:
编辑:
Once you've moved the cursor to the 3rd record as I explain above, use the following to just get the value of the "Name" column.
按照上面的说明将光标移动到第3条记录后,使用以下命令获取“Name”列的值。
String theName = c.getString(getColumnIndex("Name"));
#2
1
Cursor cursor = dbHelper.getdata();
System.out.println("colo" + cursor.getColumnCount() + ""
+ cursor.getCount());
cursor.moveToFirst();
if (cursor != null) {
while (cursor.isAfterLast() == false) {
String chktitle = title.trim().toString();
String str = cursor.getString(cursor.getColumnIndex("title"));
System.out.println("title :: "
+ cursor.getString(cursor.getColumnIndex("title")));
System.out.println("date :: "
+ cursor.getString(cursor.getColumnIndex("date")));
System.out.println("desc :: "
+ cursor.getString(cursor.getColumnIndex("desc")));
if (chktitle.equals(str) == true) {
tvAddfavorite.setText("Remove Favorite");
break;
}
cursor.moveToNext();
}
}
cursor.close();
#3
1
Add a WHERE
clause:
添加WHERE子句:
qb.appendWhere("_id = 2");
#4
0
Thanks to the answers above I created the following method to give the value of an item from another column but in the same row.
感谢上面的答案,我创建了以下方法,以便从另一列但同一行中提供项目的值。
public String getValueFromColumn(int position, String tableName, String columnToGetValueFrom) {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(tableName);
Cursor c = qb.query(db, null, null, null,
null, null, null);
c.moveToPosition(position);
String neededValue = c.getString(c.getColumnIndex(columnToGetValueFrom));
return neededValue;
}
Hope it helps anyone.
希望它能帮助任何人。