-2

I want to check if there are already 5 rows in my database, but I can't use the execSQL method to call SELECT because it returns void and nothing else seems to fit in an if statement. So is there a simple fix or do I have to go about this another way?

//add a new row to the database
public void addProduct(Products product){

    SQLiteDatabase db =  this.getWritableDatabase();
    if(db.execSQL("SELECT Count * FROM " + TABLE_PRODUCTS) == 5){
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCTNAME, product.get_productname());
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ryder Thacker
  • 1,472
  • 3
  • 13
  • 33
  • http://stackoverflow.com/questions/18097748/how-to-get-row-count-in-sqlite-using-android refer this. – Jaydeep Devda Mar 18 '17 at 04:23
  • For **queries**, use `rawQuery()`. For **commands**, use `execSQL()`. If you know the difference between a SQL query and a SQL command. – Phantômaxx Mar 18 '17 at 07:30

1 Answers1

3

Try this

SQLiteDatabase db = this.getReadableDatabase();   
if(DatabaseUtils.queryNumEntries(db, TABLE_PRODUCTS) == 5){ 
   ContentValues values = new ContentValues(); 
   values.put(COLUMN_PRODUCTNAME, product.get_productname()); 
   db.insert(TABLE_PRODUCTS, null, values); db.close(); 
 }
albeee
  • 1,452
  • 1
  • 12
  • 20