【问题标题】:SQLiteConstraintException NOT BEING CAUGHTSQLiteConstraintException 没有被捕获
【发布时间】:2013-11-11 14:33:03
【问题描述】:
//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");
        }

【问题讨论】:

  • 哪一行引发了异常?
  • 在 catch 块中使用 Throwable 而不是 Exception。

标签: android sqlite


【解决方案1】:

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

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

【讨论】:

  • 感谢您现在能够捕获但现在我正在尝试从内容解析器访问数据库以及如何从内容解析器捕获 SQLLIte 异常
  • 检查here
猜你喜欢
  • 2011-03-26
  • 2012-05-31
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多