从 android 数据库 sqlite 中获取特定数据
Fetch specific data from android database sqlite
我想知道是否可以基于 sqlite 从 android 数据库中获取一种特定类型的数据。
假设我有一个 table,行 "Category" 和 "Title",我想得到一个字符串数组,其中包含与给定类别匹配的 "Title" .
例如:
Title Category
A spam
B important
C spam
并且给定 "spam",我想得到一个像
这样的字符串数组
S = {A,C}
有办法吗?
请注意,我对数据库还很陌生。
提前致谢。
编辑:
我正在尝试查询
mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=" + category, null, null, null, KEY_CATEGORY);
但它 returns 是一个 Cursor,我需要一个像这里一样的 SimpleCursorAdapter 来格式化
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
mList.setAdapter(notes);
从哪里到哪里:
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
最终编辑:
终于成功了,代码由@ELITE
提供
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=?", new String[]{category}, null, null, KEY_CATEGORY);
ArrayList elements = new ArrayList();
while(cursor.moveToNext()) {
elements.add(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
}
// use elements variable,
// here you'll get ["A", "C"]
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, cursor, from, to);
mList.setAdapter(notes);
像这样使用 SQLiteDatabase
class 的 query()
:
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("<name of the table>", new String[]{}, "Category=?", new String[]{"spam"}, null, null, null);
遍历游标并将结果存储在ArrayList
或Vector
中然后使用它。
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=?", new String[]{category}, null, null, KEY_CATEGORY);
ArrayList elements = new ArrayList();
while(cursor.moveToNext()) {
elements.add(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
}
cursor.close();
// use elements variable,
// here you'll get ["A", "C"]
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, elements, from, to);
mList.setAdapter(notes);
希望它能奏效。
有关详细信息,请参阅 this question
我想知道是否可以基于 sqlite 从 android 数据库中获取一种特定类型的数据。
假设我有一个 table,行 "Category" 和 "Title",我想得到一个字符串数组,其中包含与给定类别匹配的 "Title" .
例如:
Title Category
A spam
B important
C spam
并且给定 "spam",我想得到一个像
这样的字符串数组S = {A,C}
有办法吗?
请注意,我对数据库还很陌生。
提前致谢。
编辑:
我正在尝试查询
mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=" + category, null, null, null, KEY_CATEGORY);
但它 returns 是一个 Cursor,我需要一个像这里一样的 SimpleCursorAdapter 来格式化
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
mList.setAdapter(notes);
从哪里到哪里:
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
最终编辑:
终于成功了,代码由@ELITE
提供Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=?", new String[]{category}, null, null, KEY_CATEGORY);
ArrayList elements = new ArrayList();
while(cursor.moveToNext()) {
elements.add(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
}
// use elements variable,
// here you'll get ["A", "C"]
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, cursor, from, to);
mList.setAdapter(notes);
像这样使用 SQLiteDatabase
class 的 query()
:
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("<name of the table>", new String[]{}, "Category=?", new String[]{"spam"}, null, null, null);
遍历游标并将结果存储在ArrayList
或Vector
中然后使用它。
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=?", new String[]{category}, null, null, KEY_CATEGORY);
ArrayList elements = new ArrayList();
while(cursor.moveToNext()) {
elements.add(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
}
cursor.close();
// use elements variable,
// here you'll get ["A", "C"]
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, elements, from, to);
mList.setAdapter(notes);
希望它能奏效。
有关详细信息,请参阅 this question