Android:在sqlite中搜索三列

时间:2013-11-24 19:15:29

标签: android sqlite

我编写一个应用程序,记录所有连接的单元格的单元格ID并将值写入数据库

ID COLUMN_OP_ID LAC CID

x xxxxx xxxxx xxxxxxx

我的代码:

  public String findProduct(String lacId) {
        String query = "Select * FROM " + TABLE_TOWERS + " WHERE " + COLUMN_LAC + " =  \"" + lacId + "\"";

        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.rawQuery(query, null);

        String lac = null;

        if (cursor.moveToFirst()) {
            cursor.moveToFirst();
            lac =cursor.getString(2);
            cursor.close();
        } else {
            lac = null;
        }
            db.close();
        return lac;
    }    

然而,使用此代码,我得到包含此no的所有行。我想实现一个函数来搜索Op_Id,lac和cid的所有三列,以缩小搜索范围并进一步缩小搜索范围。但是,我无法搜索所有三列以缩小搜索范围

例如

 public String findRecord(String id, String lac, String cid)

如何设计这样的功能? 谢谢,Sahil

1 个答案:

答案 0 :(得分:0)

完成了工作。

 public String findID(String lacId, String opId, String cid) {
        String query = "Select * FROM " + TABLE_TOWERS + " WHERE " + COLUMN_LAC + " = \"" + lacId + "\" AND " + COLUMN_OPERATOR_ID + " = \"" + opId + "\" AND " + COLUMN_CID + " = \"" + cid + "\"";

        System.out.println(query);
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.rawQuery(query, null);

        String id= null;

        if (cursor.moveToFirst()) {
            cursor.moveToFirst();
            lac =cursor.getString(0);
            cursor.close();
        } else {
            id = null;
        }
            db.close();
        return id;
    }