0

我有三个表,我想用 sql 语句进行查询,但它不能很好地工作,因为它没有给我所有可能的结果......

编码:

public void setsuchresultat(String pSearchword)
{

    String[] table = new String[]{"tbl_A", "tbl_B", "tbl_C"};
    String[] column = new String[]{"columnA", "columnB" }; // Type varchar(50) not null     

    String[] search = new String[]{"%" + pSearchword+ "%", pSearchword+ "%", "%" + pSearchword, pSearchword};
    getdata(table,column,search);
}

public void getdata(String[] pTable, String[] pColumn, String[] pSearch)
{
    for(String t:pTable)
    {
        for(String s : pColumn)
        {
            for(String k : pSearch)
            {
                Cursor c = this.db.rawQuery("SELECT * FROM " + t + " WHERE " + s + " LIKE '"+ k + "'", null); 
                if ((c.moveToFirst()) || c.getCount() > 0)
                {
                    while(c.moveToNext())// Suchresultat abspeichern
                    {
                        String col1 = c.getString(c.getColumnIndex("columnA"));
                        String col2 = c.getString(c.getColumnIndex("columnB"));
                    }
                }
            }
        }
    }       
}   
4

1 回答 1

0

我认为问题出在你的循环中,如果检查。

改变

if ((c.moveToFirst()) || c.getCount() > 0)
                {
                    while(c.moveToNext())// Suchresultat abspeichern
                    {
                        String col1 = c.getString(c.getColumnIndex("columnA"));
                        String col2 = c.getString(c.getColumnIndex("columnB"));
                    }
                }

if (c.moveToFirst() || c.getCount() > 0) /* OR if (c != null && c.moveToFirst())*/ {
     do {
         String col1 = c.getString(c.getColumnIndex("columnA"));
         String col2 = c.getString(c.getColumnIndex("columnB"));
     } while(c.moveToNext());
}
于 2013-11-15T08:24:39.530 回答