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