1

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

3
  • if Name is string, you need to use quotes Commented Aug 28, 2019 at 7:30
  • 1
    share the code of create table. Commented Aug 28, 2019 at 7:32
  • You could do the same thing with the UNIQUE constraint (which is much safer to use than custom code) Commented Aug 28, 2019 at 7:35

1 Answer 1

1

Try this Query

db.rawQuery("SELECT * FROM Brand_Table WHERE Name='" + brandName+ "'",null);

instead of this what you have tried

db.rawQuery("SELECT * FROM Brand_Table WHERE Name="+brandName, null);

branName is a String needs to be in single quote.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.