0
//MAINACTIVITY.JAVA


idView = (TextView) findViewById(R.id.lblId);
nameBox = (EditText) findViewById(R.id.txtName);
rollnoBox = (EditText) findViewById(R.id.txtRollNo);
classBox = (EditText) findViewById(R.id.txtClass);
marksBox = (EditText) findViewById(R.id.txtMarks);


public void newStudent (View view) {
    MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);

    try {

        int marks = Integer.parseInt(marksBox.getText().toString());

        //JUST TO MAKE SURE FIELDS ARE NOT EMPTY
        if(nameBox.getText().toString().isEmpty() || rollnoBox.getText().toString().isEmpty() || classBox.getText().toString().isEmpty()){
            Toast.makeText(this, "Error" , Toast.LENGTH_SHORT).show();
            return;
        }

        //Added Trim TO REMOVE EXTRA SPACES
        Student student = new Student(nameBox.getText().toString().trim(), rollnoBox.getText().toString().trim(), classBox.getText().toString().trim(), marks);
        dbHandler.addStudent(student,MainActivity.this);
        Toast.makeText(this, student.getName() + " Added Successfully" , Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Log.i("hellolog", "EXEPTION CAUGHT 0");
        Toast.makeText(this, "Error" , Toast.LENGTH_SHORT).show();
    }

    //After adding Clear the fields
    nameBox.setText("");
    rollnoBox.setText("");
    classBox.setText("");
    marksBox.setText("");

}



//DBHANDLER.JAVA

public void onCreate(SQLiteDatabase db) {

    //collate nocase -> IS TO PERFORM CASE INSENSTIVE SEARCH
    String CREATE_STUDENTS_TABLE = "CREATE TABLE " + TABLE_STUDENTS + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
            + COLUMN_NAME + " TEXT collate nocase, " + COLUMN_ROLLNO + " TEXT collate nocase UNIQUE, " + COLUMN_CLASS + " TEXT, " + COLUMN_MARKS + " INTEGER" + ")";
    db.execSQL(CREATE_STUDENTS_TABLE);

}



public void addStudent(Student student, Context context) {
    SQLiteDatabase db=null;
    ContentValues values  = new ContentValues();
    values.put(COLUMN_NAME, student.getName());
    values.put(COLUMN_ROLLNO, student.getRollno());
    values.put(COLUMN_CLASS, student.getSclass());
    values.put(COLUMN_MARKS, student.getMarks());

    try {
        db = this.getWritableDatabase();
        db.insert(TABLE_STUDENTS, null, values);
    } 
    catch(SQLiteConstraintException e) {
        Log.i("hellolog", "EXEPTION SQLiteConstraintException 1");
        Toast.makeText(context.getApplicationContext(), "INVALID ROLL NO" , Toast.LENGTH_SHORT).show();
    }
    catch (Exception e) {
        Log.i("hellolog", "EXEPTION CAUGHT 2");
        Toast.makeText(context.getApplicationContext(), "INVALID" , Toast.LENGTH_SHORT).show();
    }

    finally {
        db.close();
    }


}

一个简单的学生数据库,其中 ROLLNO 字段是唯一的。我正在尝试显示敬酒消息。如果用户在文本字段中提供已经存在的卷号,则 ROLLNO 已经存在。但我正在进入SQLiteConstraintExceptionLogcat。但是,以下代码行

Log.i("hellolog", "EXEPTION SQLiteConstraintException 1"); 

未显示在 Logcat 中。不执行 Catch 块。并且应用程序运行正常,没有被Android系统终止SQLiteConstraintException

//I'm Using this for Showing Error message  to USER
if(db.insert(TABLE_STUDENTS, null, values)!=-1) {
            //db.insert(TABLE_STUDENTS, null, values);
            Toast.makeText(context, student.getName() + " Added Successfully" , Toast.LENGTH_SHORT).show();

        }
        else {
            Toast.makeText(context.getApplicationContext(), "ERROR INSERTING" , Toast.LENGTH_SHORT).show();
            Log.i("hellolog", "EERROR");
        }
4

1 回答 1

0

检查您尝试COLUMN_ROLLNO仅使用 UNIQUE 插入数据的天气。如果您尝试插入已存在的COLUMN_ROLLNO内容,则会发生此异常。

db.insertOrThrow(TABLE_STUDENTS, null, values);像这样更改您的插入语句。然后再试一次。让我知道发生什么事。

于 2013-11-11T06:32:24.820 回答