我想比较两个不同的表中的名称,然后等于获取详细信息

时间:2012-02-20 08:53:16

标签: android

我有两个不同的表。我想比较两个表中的名称,如果两个名称相等,那么我想得到详细信息。我可以这样做吗?请帮帮我。

以下是代码:

Cursor cr=mDbManager.fetchnewcustomerdata();
        Cursor cr1 = mDbManager.fetchinvoicecustomerdata();
        cr1.moveToFirst();
        //String address;
        while (!(cr1.isAfterLast())) {
            String name = cr1.getString(cr1.getColumnIndex("icstname"));
            if(name.equals(cr.getString(cr.getColumnIndex("cname"))))
            {
                 Toast.makeText(this,"equal",1000).show();
            }
            String address = cr1.getString(cr1.getColumnIndex("caddress"));
            map.put(name, address);
            nameAList.add(cr1.getString(cr1.getColumnIndex("icstname")));
            cr1.moveToNext();
        }
        mDbManager.close();
}

3 个答案:

答案 0 :(得分:0)

我认为有两种方式可行

1) Either you need to establish relationship between those tables using the name column you are thinking of same between two tables.

2) Two separate queries to database, where condition for both queries will be the name 

答案 1 :(得分:0)

您可以使用CursorJoiner

http://developer.android.com/reference/android/database/CursorJoiner.html

答案 2 :(得分:0)

首先,这些名字是有序的。  然后,将其排序为以下示例:

 while( !eofInTable1 && !eofInTable2) {
        String name1 = getFieldFromTable1();
        String name2 = getFiledFromTable2();
        int tmpResult = name1.compareTo(name2);
        if(tmpResult == 0) {
             doYourAction();
             table1.moveToNext();
             table2.moveToNext();
        }
        else if( tmpResult == -1) {
           table1.moveToNext();
        }
        else {
           table2.moveToNext();
         }

  }