在android中使用游标从数据库表中获取数据

时间:2011-06-02 13:34:05

标签: android

我有一个employee table,其中包含列值

("store_loc_id","name","password","address_id","role_id",
"retailer_id").

我想使用游标从此表中获取namepassword。我怎么办?

2 个答案:

答案 0 :(得分:1)

没有检查这段代码,但这个成语应该是可以理解的。

Cursor cursor = db.query(...);
if(cursor !=null && cursor.moveToFirst()) {
   do {
     String name = cursor.getString(cursor.getColumnIndex("name"));
   } while (cursor.moveToNext());
}

答案 1 :(得分:0)

public Cursor getEmployee() {
    try {
        return db.query(employee, new String[] { "name", "password" },
                null, null, null, null, null);
    } catch (SQLException e) {
        Log.e("Exception on query", e.toString());
        return null;
    }
}


Cursor c = db.getEmployee();
    if (c == null) {
        // do nothing
    } else {
        if (c.getCount() > 0) {
            if (c.moveToFirst()) {
                do {
                    String Name = c.getString(0);
                    String Password = c.getString(1);
                } while (c.moveToNext());
            }
        } 
}