将列与数据库中的String进行比较?

时间:2012-06-15 01:09:19

标签: android string sqlite

我需要帮助将列与字符串进行比较。 android数据库中有一个列,它以字符串形式存储和关闭。我需要将列与“On”字符串进行比较,如果该列中有任何On,我需要它返回true,如果数据库中没有on,则返回false,我该怎么做?

我的代码

  

public boolean QuickTime(){

      String myQuery = "SELECT * FROM DATABASE_TABLE_QUICK WHERE ALARM_ID_QUICK LIKE 'On' ";
      Cursor mCursor = databaseConnect.rawQuery(myQuery, null);
      if(mCursor.getCount()>0)  //if at least one record contains 'on'
          return true;
      else
          return false; //no record was found with the word 'on'
  }

2 个答案:

答案 0 :(得分:0)

一种简单(不是最好)的方法是使用Rawquery。 所以你所要做的就是:

private boolean containsOn(SQLiteDatabase mSQLiteDatabase){
    String myQuery = "SELECT * FROM MYTABLE WHERE status LIKE 'on' ";
    Cursor mCursor = mSQLiteDatabase.rawQuery(myQuery, null);
    mCursor.
    if(mCursor.getCount()>0)  //if at least one record contains 'on'
        return true;
    else
        return false; //no record was found with the word 'on'
}

您需要做的就是传递sqlLite数据库,更改列名的表名和状态列。

答案 1 :(得分:0)

sqlite> create table on_c(id int,onoff varchar(3));
sqlite> insert into on_c (id,onoff)values(1,'ON');
sqlite> insert into on_c (id,onoff)values(2,'OFF');
sqlite> select * from on_c;
1|ON
2|OF
select case count(*) when 0 then 'true' else 'false' end from on_c where onoff like '%N';
false