//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已存在。但我在Logcat中得到SQLiteConstraintException
。但是,以下代码行
Log.i("hellolog", "EXEPTION SQLiteConstraintException 1");
未显示在Logcat中。 未执行Catch块。并且应用程序正常运行,而不是由SQLiteConstraintException
上的Android系统终止。
//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");
}
答案 0 :(得分:0)
检查您正在尝试使用UNIQUE COLUMN_ROLLNO
插入数据的天气。如果您尝试插入已存在的COLUMN_ROLLNO
,则会发生此异常。
db.insertOrThrow(TABLE_STUDENTS, null, values);
像这样更改插入语句。然后再试一次。让我知道会发生什么。