Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6946969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:38:00+00:00 2026-05-27T13:38:00+00:00

to prevent user inputs the same data into database i use this db.execSQL(CREATE UNIQUE

  • 0

to prevent user inputs the same data into database i use this

db.execSQL("CREATE UNIQUE INDEX DIFFERENT ON user(name, gender)");

My question is how can i show the error to end users that tell them the data that they input exists already? Or there is another way to prevent the user to input same data and show the error to the user?

DatabaseUsername.java (where i put unique index)

public class DatabaseUsername extends SQLiteOpenHelper {
private static final String DATABASE_NAME="rikartoiy.db";
private static final int SCHEMA_VERSION=1;


public DatabaseUsername(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE user (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, gender TEXT);");
    db.execSQL("CREATE TABLE score (_id INTEGER PRIMARY KEY AUTOINCREMENT, score INTEGER, dateTaken TEXT, userId INTEGER NOT NULL, FOREIGN KEY (userId) REFERENCES user(_id) ON DELETE CASCADE);"); //create table score
    db.execSQL("CREATE UNIQUE INDEX DIFFERENT ON user(name, gender)")


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // no-op, since will not be called until 2nd schema
    // version exists

}

public Cursor getAll() {
    return(getReadableDatabase()
                    .rawQuery("SELECT _id, name, gender FROM user ORDER BY name",
                                        null));
}

public Cursor getCurScore() {
    return(getReadableDatabase()
                    .rawQuery("SELECT alm._id, alm.name, alm.gender, sc._id, sc.score, sc.userId FROM user alm, score sc WHERE sc.userId = alm._id AND alm._id = ?",
                                        null));
}

public Cursor getById(String id) {
    String[] args={id};

    return(getReadableDatabase()
                    .rawQuery("SELECT _id FROM user WHERE _ID=?",
                                        args)); 
}

public void insert(String name, String gender) {
    ContentValues cv=new ContentValues();

    cv.put("name", name);
    cv.put("gender", gender);

    getWritableDatabase().insert("user", name, cv);
}

public void insertMark(int score, int userId, String dateTaken) {
    ContentValues cv=new ContentValues();

    cv.put("score", score);
    cv.put("userId", userId);
    cv.put("dateTaken", dateTaken);

    getWritableDatabase().insert("score", null, cv);
}

public String getName(Cursor c) {
    return(c.getString(1));
}

public String getGender(Cursor c) {
    return(c.getString(2));
}

public int getScore(Cursor c) {
    return(c.getInt(3));
}}

UsernameRegister.java(where user inputs the data)

public class UsernameRegister extends Activity {
EditText name=null;
RadioGroup gender=null;
DatabaseUsername helper=null;
String almagId=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.usernameregister);

    helper=new DatabaseUsername(this);

    name=(EditText)findViewById(R.id.name);
    gender=(RadioGroup)findViewById(R.id.gender);

    Button save=(Button)findViewById(R.id.save);

    save.setOnClickListener(onSave);

    almagId=getIntent().getStringExtra(UsernameList.ID_EXTRA);

    if (almagId!=null) {
        load();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();

    helper.close();
}

private void load() {
    Cursor c=helper.getById(almagId);

    c.moveToFirst();        
    name.setText(helper.getName(c));

    if (helper.getGender(c).equals("Pria")) {
        gender.check(R.id.pria);
    }
    else if (helper.getGender(c).equals("Perempuan")) {
        gender.check(R.id.perempuan);
    }


    c.close();
}

private View.OnClickListener onSave=new View.OnClickListener() {
    public void onClick(View v) {
        String type=null;

        switch (gender.getCheckedRadioButtonId()) {
            case R.id.pria:
                type="Pria";
                break;
            case R.id.perempuan:
                type="Perempuan";
                break;

        }

        String namanama = name.getText().toString();

        if (almagId==null) {
            helper.insert(namanama, type);



            if(TextUtils.isEmpty(namanama)) 
            { 
                name.setError("Your name can't be empty"); 
                return; 
            }

            else if (namanama.length() < 3 ) 
            { 
                name.setError("Your name can't less than 3"); 
                return; 
            }

            else if (namanama.length() > 10 ) 
            { 
                name.setError("Your name can't more than 3"); 
                return; 
            }

        }


        finish();
    }
};}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:38:01+00:00Added an answer on May 27, 2026 at 1:38 pm

    You can use the return value of mDb.insert();

    Returns
    
        the row ID of the newly inserted row, or -1 if an error occurred 
    

    if it returns -1, AND you are sure that the error could not come from something else than duplicate values, tell the user values already exist.

    Or you can create a function that looks for the values entered by user in database, if these values exist, show a message to user before inserting them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I prevent a user from tabbing into a field (comboxbox)?
how do I prevent the user to change the value in an input field
I need to prevent user from selecting text (select all or select a portion
Is it possible to prevent user from writing letters to a textbox (i.e. force
In wpf how can i prevent user from moving the windows by dragging the
After processing form from POST I should redirect, to prevent user from hitting back.
How can I prevent a user from resizing GridViewColumns withing a ListView control?
How can I prevent the user from being able to resize an image in
I want to prevent the user from maximizing the Windows Form to full screen
Is there a way to prevent the user from moving the cursor in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.