如何从Activity中的getAll()方法获取数据

时间:2015-11-03 10:12:00

标签: java android sqlite

数据库结构:

明细

 rid     name
  1      Beta
  2      Release

记录

 recid     rname   conrid (conrid is rid from Detail table)
   1       App1      1
   2       App2      1
   3       App3      2
   4       App4      1

我编写了一种方法,可以根据ridconrid从两个表(详细信息和记录)中获取所有数据。
这是代码:

String select = "SELECT rname FROM " + TABLE_RECORD+ " where conrid =" +detail.getId();      

2 个答案:

答案 0 :(得分:2)

看来你已经关闭了一个循环,你的尾巴......左右 因此,我将您的代码与@MD提供的答案混合在一起。

我相信他并没有把它弄坏。 ;)

您需要添加一个名为RecDet的类(包含Detail中的Name字段和Record中的RName)。

使用该代替DetailRecord

// to get all data from Detail and Record tables
public List<RecDet> getAll()
{
    List<RecDet> listRecDet = new ArrayList<RecDet>();

    // Select All Query
    String selectQuery =
        "SELECT Details.name, Record.rname FROM Detail INNER JOIN Record " +
        "ON Record.conrid = Detail.rid";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor != null && cursor.moveToFirst())
    {
        do
        {
            RecDet recdet = new RecDet();
            recdet.setName(cursor.getString(cursor.getColumnIndex("name")));
            recdet.setRname(cursor.getString(cursor.getColumnIndex("rname")));
            listRecDet.add(recdet);
        } while (cursor.moveToNext());
    }
    return listRecDet;
}

答案 1 :(得分:1)

我们试试这个

select Details.name,Record.rname from Detail INNER JOIN Record ON Record.conrid=Detail.rid