数据库查询 返回元素是数组的List 要取得数组内的元素的值
查询语句
public List query(){
db = helper.getReadableDatabase();
ArrayList objs = new ArrayList();
Cursor c = queryTheCursor();
while (c.moveToNext()){
Object[] obj = new Object[]{c.getString(c.getColumnIndex("name")),//角标0
c.getString(c.getColumnIndex("uid")),//1
c.getInt(c.getColumnIndex("year")),//2
c.getInt(c.getColumnIndex("month")),//3
c.getInt(c.getColumnIndex("day")),//4
c.getString(c.getColumnIndex("post")),//5 职业
c.getInt(c.getColumnIndex("sex"))};//6
objs.add(obj);
}
c.close();
db.close();
return objs;
}
/**
* query all persons, return cursor
*
* @return Cursor
*/
public Cursor queryTheCursor()
{
db = helper.getReadableDatabase();
//Cursor c = db.rawQuery("SELECT * FROM person", null);
Cursor c = db.rawQuery("select name,uid,year,month,day,sex,post from person p join job j on p.jid=j.jid",null);
return c;
}
调用查询语句
mDbManager = new DBManager(this);//实例化对象
List objects = mDbManager.query();//从数据库取得全部用户信息
获取数组中的姓名
/********获取姓名********/
String[] name = new String[objects.size()];
for (int i = 0; i
{
Object[] objects2 = objects.get(i);//得到所有obj数组
name[i] = (String) objects2[0];//给姓名数组元素赋值
}
for (int j = 0; j
{
System.out.println("===========================================name[]"+name[j]);
}