Search code examples
androidsqliteandroid-sqlite

How to check whether the value is inserted or not in SQLite?


How to check whether the value is inserted or not in Android?

I want to display

if inserted==true
   return true;
else
   return false;

This is my code:

SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
sqLiteDatabase.execSQL("INSERT INTO user VALUES (1,1,1,'admin','admin','21232f297a57a5a743894a0e4a801fc3','9986058114','8897456588','admin@admin.com','','Active','AD','admin')");

Solution

  • Check By DataBase : https://stackoverflow.com/a/21151598/8448886

    You can also use Log to check data is inserted on or not on your DataBaseHelper class.

      public boolean addData(String name , String sku, String upc , String price, String displaySize, int displaySizeYes, String status){
        db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Constants.NAME, name );
        contentValues.put(Constants.SKU, sku );
        contentValues.put(Constants.UPC, upc );
        contentValues.put(Constants.PRICE, price );
        contentValues.put(Constants.DISPLAY_SIZE, displaySize );
        contentValues.put(Constants.DISPLAY_SIZE_YES, displaySizeYes );
        contentValues.put(Constants.STATUS, status );
    
        long result = db.insert(Constants.TABLE_NAME,null,contentValues);
        Log.e(TAG,""+upc+"  Inserted");
    
        if(result == -1) {
            return false;
        }else{
          //  Log.e(TAG,"value inserted");
            return true;
        }
    
    }