0

I want to check whether a row of the sqlite table exists in database or not..If row exists i should call a class otherwise another class.To get this functionality i am using

  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;
    }

but using this code the rows are reinserting ..Could anyone suggest me the way to approach the functionality

2 Answers2

2

Your equality check is failing because this expression is evaluating to true:

if(exists=true)

When you have a single equals sign, it means assignment. You are assigning true to exists, instead of comparing exists to true. The result of the assignment is the value to which exists was assigned (true).

Try the proper equality operator instead:

if (exists == true)

See this related post for another look at this issue.

Community
  • 1
  • 1
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
0

Here the issue is in if(exists=true). Need to change to if (exists == true). You can also check using cursor.getCount(); It will returns the numbers of rows in the cursor

Dennis Mathew
  • 408
  • 5
  • 14