Shubham Hingne
Shubham Hingne

Reputation: 71

How to prevent duplicate entry in sqlite

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

Upvotes: 1

Views: 76

Answers (1)

Rajnish suryavanshi
Rajnish suryavanshi

Reputation: 3434

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.

Upvotes: 1

Related Questions