【问题标题】:how to check whether row exists or not in a table of sqlite for android如何检查Android的sqlite表中是否存在行
【发布时间】:2012-11-23 04:00:33
【问题描述】:

我想检查sqlite表的一行是否存在于数据库中。如果存在行,我应该调用一个类,否则调用另一个类。为了获得这个功能,我正在使用

  public boolean Exists(int position) {

       SQLiteDatabase db =  (placeData).getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from pages where bbookid ='"+ position +"'", null);

       boolean exists = (cursor.getColumnName(position)) != null;
       Log.i("logs","fads");
       if(exists=true)
       {
           getDataAndPopulate();
       }
       else
       {
       Log.i("qefw","cursors");
       new LoadAllProducts().execute();
       }
       cursor.close();
       return exists;
    }

但是使用此代码,行正在重新插入..有人可以建议我使用该功能的方法

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    您的相等性检查失败,因为此表达式的计算结果为 true

    if(exists=true)
    

    当你有一个等号时,它意味着赋值。您将true 分配给exists,而不是将existstrue 进行比较。分配的结果是分配给 exists 的值 (true)。

    尝试使用适当的相等运算符:

    if (exists == true)
    

    请参阅this related post 以重新了解此问题。

    【讨论】:

      【解决方案2】:

      这里的问题在于 if(exists=true)。需要更改为 if (exists == true)。 您也可以使用 cursor.getCount();它将返回游标中的行数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-04
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多