i need to prevent duplicates entry in sqlite i made this code but it not working.
String brandName=BrandName.getText().toString().trim();
SQLiteDatabase db = dbh.getWritableDatabase();
Cursor c=db.rawQuery("SELECT * FROM Brand_Table WHERE Name="+brandName, null);
if(c.moveToFirst())
{
showMessage("Error", "Record exist");
}
else
{
ContentValues values = new ContentValues();
values.put("Name", brandName);
db.insert("Brand_Table", null, values);
Toast.makeText(getActivity(), "Brand Created", Toast.LENGTH_LONG).show();
BrandName.setText("");
}
Error Logs
android.database.sqlite.SQLiteException: no such column: abc (code 1): , while compiling: SELECT * FROM Brand_Table WHERE Name=abc
my database look likes
id Name
1 abc
2 pqr
id is auto incremented i am passing value abc from edittext and data is present in database but it gives above error and stop application
Nameis string, you need to use quotesUNIQUEconstraint (which is much safer to use than custom code)