Android database store same name in database issue

358 Views Asked by At

I am new to android and I need help here: I have a database which works fine. The problem is that I have to save same values in this database. I want to check if the name already exists in this database. If so it should show you an error:

"name already exist".

Thanks.

//dbhelper class

public class FoodDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "pkfood_calories.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
        "CREATE TABLE "+ Food.NewDishInfo.TABLE_NAME+"("
                + Food.NewDishInfo.DISH_NAME+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_QUANTITY+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_CALORIE+" INTEGER,"
                + Food.NewDishInfo.DISH_FAT+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_PROTEIN+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_SUGAR+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_VITAMINS+" TEXT NOT NULL);";
public FoodDbHelper(Context context)
{
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    Log.e("DATABASE OPERATION","Database created / opened...");
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_QUERY);
    Log.e("DATABASE OPERATION","Table created...");


}
public void addInformations(String name ,String quantity, Integer calorie, String fat ,
                            String protein,String sugar,String vitamins, SQLiteDatabase db){


    ContentValues contentValues = new ContentValues();
    contentValues.put(Food.NewDishInfo.DISH_NAME,name);
    contentValues.put(Food.NewDishInfo.DISH_QUANTITY,quantity);
    contentValues.put(Food.NewDishInfo.DISH_CALORIE,calorie);
    contentValues.put(Food.NewDishInfo.DISH_FAT,fat);
    contentValues.put(Food.NewDishInfo.DISH_PROTEIN,protein);
    contentValues.put(Food.NewDishInfo.DISH_SUGAR,sugar);
    contentValues.put(Food.NewDishInfo.DISH_VITAMINS,vitamins);
    db.insert(Food.NewDishInfo.TABLE_NAME, null, contentValues);
    Log.e("DATABASE OPERATION","one row inserted...");
}



//activity code

name = (EditText) findViewById(R.id.dish_name);
    quantity = (EditText) findViewById(R.id.dish_quantity);

    calorie = (EditText) findViewById(R.id.dish_calorie);
    fat = (EditText) findViewById(R.id.dish_fat);
    protein = (EditText) findViewById(R.id.dish_protein);
    sugar = (EditText) findViewById(R.id.dish_sugar);
    vitamins = (EditText) findViewById(R.id.dish_vitamins);
}

public void addDish(View view) {

    String dish_name = name.getText().toString();
    String dish_quantity = quantity.getText().toString();
   // dish_calorie = Integer.parseInt(calorie.getText().toString());
    String dish_fat = fat.getText().toString();
    String dish_protein = protein.getText().toString();
    String dish_sugar = sugar.getText().toString();
    String dish_vitamins = vitamins.getText().toString();

    if (name.getText().toString().matches("")) {
        name.setError("please enter dish name.");
        return;
    }
    if(quantity.getText().toString().matches("")) {
        quantity.setError("please enter dish quantity.");
        return;
    }
        if (calorie.getText().toString().matches("")) {
            calorie.setError("please enter dish calories.");
        return;
    }
    if (fat.getText().toString().matches("")) {
        fat.setError("please enter dish fats.");
        return;
    }
    if (protein.getText().toString().matches("")) {
        protein.setError("please enter dish proteins.");
        return;
    }
    if (sugar.getText().toString().matches("")) {
        sugar.setError("please enter dish sugar.");
        return;
    }
    if (vitamins.getText().toString().matches("")) {
        vitamins.setError("please enter dish vitamins.");
        return;
    }

            else {

        foodDbHelper = new FoodDbHelper(context);
        sqLiteDatabase = foodDbHelper.getWritableDatabase();
        foodDbHelper.addInformations(dish_name, dish_quantity, Integer.parseInt(calorie.getText().toString()), dish_fat,
                dish_protein, dish_sugar, dish_vitamins, sqLiteDatabase);
        Toast.makeText(getBaseContext(), "Data Saved", Toast.LENGTH_SHORT).show();
        foodDbHelper.close();
        Intent intent = new Intent(this, AddNewDish.class);
        startActivity(intent);


    }

}
2

There are 2 best solutions below

3
On

Make your name unique:

private static final String CREATE_QUERY =
    "CREATE TABLE "+ Food.NewDishInfo.TABLE_NAME+"("
            + Food.NewDishInfo.DISH_NAME+" UNIQUE TEXT NOT NULL," //here
            + Food.NewDishInfo.DISH_QUANTITY+" TEXT NOT NULL,"
            + Food.NewDishInfo.DISH_CALORIE+" INTEGER,"
            + Food.NewDishInfo.DISH_FAT+" TEXT NOT NULL,"
            + Food.NewDishInfo.DISH_PROTEIN+" TEXT NOT NULL,"
            + Food.NewDishInfo.DISH_SUGAR+" TEXT NOT NULL,"
            + Food.NewDishInfo.DISH_VITAMINS+" TEXT NOT NULL);";

And than when u try to add already existing name u will get exception.

Also after changing your database u need to clear data from app, to let app create database again.

4
On

You have two options for this

  1. make name as primary key. When this column made primary key, duplicate value can not be inserted there. You can put your insert in try catch statement. And in the exception check whether it is "data integrity exception" or not. If yes show your message.
  2. Write one select method having name in where clause returning count of rows for that name . If it is 0, insert your record, otherwise show your message.

Hope this helps you.