mardi 3 novembre 2015

How to make Join and Fetch Data from Multiple tables

I am getting data from SQLite tables,using following code:

Getting data from Detail table:

public List<Detail> getAllD() {

    List<Detail> listDetail = new ArrayList<Detail>();       

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_DETAIL;

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Detail detail = new Detail();
            detail.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex("rid"))));                
            listDetail.add(detail);                  
        } while (cursor.moveToNext());
    }

    return listDetail;
}

Getting data from Record table:

public List<Record> getAllR() {

    List<Record> listRecord = new ArrayList<Record>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_RECORD;

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

    if (cursor.moveToFirst()) {
        do {
            Record record = new Record();
            record.setRecid(cursor.getLong(cursor.getColumnIndex("recid")));    
            record.setRname(cursor.getString(cursor.getColumnIndex("rname")));
            record.setRstatus(cursor.getString(cursor.getColumnIndex("rstatus")));
            listRecord.add(record);

        } while (cursor.moveToNext());
    }

    return listRecord;
}

And then in Activity fetching data from both the tables:

 buttonFetch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // From Detail Table

            List<Detail> detail = dh.getAllD();
             for (Detail dl : detail) {
                strDetailId = "Id: " + dl.getId();
                Log.d("id-value: ", strDetailId);
            }

            // from Record table
            List<Record> record = dh.getAllR();
            for (Record rd : record) {
                recordId = rd.getRecid();
                Log.d("id: ", ""+recordId); 
                strRecordName = "name: " + rd.getRname();
                Log.d("value: ", strRecordName);                    

                boolean strDataExist = dh.Exists(recordId);
                if(strDataExist) {                          
                    dh.UpdateData(recordId, "Done");                        
                }                    

                strRecordStatus = "status: " + rd.getRstatus();
                Log.d("status: ", strRecordStatus);

            }

        }

    });

As you can see in my above code, I am using two separate codes (loops) to get data from Detail table and to get data from Record table.

But I have to fetch data from both the tables in a single code (loop), because rid in detail table is same as conrid in record table, and i have to fetch data based on their relation

Like I would like to fetch data from database where rid equals to conrid

Detail table

 rid     name
  1      Beta
  2      Release

Record table

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

Aucun commentaire:

Enregistrer un commentaire