Cursor 在 android 中如何工作
How does Cursor work in android
我很难准确地可视化程序中的 'Cursor' 功能。我有点明白了,但是谁能详细解释一下它的功能?
我所说的 Cursor 是指 Cursor
界面。
我不能简单地理解它对任何事物的作用。
http://developer.android.com/reference/android/database/Cursor.html
您指的是这个 Cursor 用法吗?
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
//Cursor points to a location in your results
Cursor c = db.rawQuery(query, null);
//Move to the first row in your results
c.moveToFirst();
In computer science, a database cursor is a control structure that enables traversal over the records in a database. Cursors facilitate subsequent processing in conjunction with the traversal, such as retrieval, addition and removal of database records. The database cursor characteristic of traversal makes cursors akin to the programming language concept of iterator.
来自 Here
A cursor is a tool that allows you to iterate the records in a set. It has concepts of order and current record.
来自The documentation you pointed yourself
provides random read-write access to the result set returned by a database query.
所以不要将 Cursor
视为一种功能,而是一种以更有效的方式从任何数据库获取记录的手段。
游标对象是从对 SQLite 数据库的查询中 return 得到的。
它将 return 查询 returns.
的所有行
假设您的数据库数据库中有一个名为 names
的 table,配置如下:
_id _name
1 Space Ghost
2 Zorak
3 Moltar
4 Brak
如果你想从这个 table 中获取所有数据并使用它,你会这样做
像这样:
public HashMap<Integer, String> getNames(){
HashMap<Integer, String> data = new HashMap<Integer, String>();
try{
SQLiteOpenHelper helper = new MyOpenDbHelper(context);
SQLiteDatabase db = helper.getReadableDatabase();
String selectQuery = "SELECT * FROM names";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null && cursor.moveToFirst()){ //make sure you got results, and move to first row
do{
int mID = cursor.getInt(0); //column 0 for the current row
String mName = cursor.getString(1); //column 1 for the current row
data.put(mID, mName);
} while (cursor.moveToNext()); //move to next row in the query result
}
} catch (Exception ex) {
Log.e("MyApp", ex.getMessage());
} finally
{
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
return data;
}
通常您会创建自己的 class 来扩展 SQLiteOpenHelper,例如:
public class MyOpenDbHelper extends SQLiteOpenHelper {
//........
}
我很难准确地可视化程序中的 'Cursor' 功能。我有点明白了,但是谁能详细解释一下它的功能?
我所说的 Cursor 是指 Cursor
界面。
我不能简单地理解它对任何事物的作用。
http://developer.android.com/reference/android/database/Cursor.html
您指的是这个 Cursor 用法吗?
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
//Cursor points to a location in your results
Cursor c = db.rawQuery(query, null);
//Move to the first row in your results
c.moveToFirst();
In computer science, a database cursor is a control structure that enables traversal over the records in a database. Cursors facilitate subsequent processing in conjunction with the traversal, such as retrieval, addition and removal of database records. The database cursor characteristic of traversal makes cursors akin to the programming language concept of iterator.
来自 Here
A cursor is a tool that allows you to iterate the records in a set. It has concepts of order and current record.
来自The documentation you pointed yourself
provides random read-write access to the result set returned by a database query.
所以不要将 Cursor
视为一种功能,而是一种以更有效的方式从任何数据库获取记录的手段。
游标对象是从对 SQLite 数据库的查询中 return 得到的。 它将 return 查询 returns.
的所有行假设您的数据库数据库中有一个名为 names
的 table,配置如下:
_id _name
1 Space Ghost
2 Zorak
3 Moltar
4 Brak
如果你想从这个 table 中获取所有数据并使用它,你会这样做 像这样:
public HashMap<Integer, String> getNames(){
HashMap<Integer, String> data = new HashMap<Integer, String>();
try{
SQLiteOpenHelper helper = new MyOpenDbHelper(context);
SQLiteDatabase db = helper.getReadableDatabase();
String selectQuery = "SELECT * FROM names";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null && cursor.moveToFirst()){ //make sure you got results, and move to first row
do{
int mID = cursor.getInt(0); //column 0 for the current row
String mName = cursor.getString(1); //column 1 for the current row
data.put(mID, mName);
} while (cursor.moveToNext()); //move to next row in the query result
}
} catch (Exception ex) {
Log.e("MyApp", ex.getMessage());
} finally
{
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
return data;
}
通常您会创建自己的 class 来扩展 SQLiteOpenHelper,例如:
public class MyOpenDbHelper extends SQLiteOpenHelper {
//........
}