android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
在android进行单元测试时,测试了自己编写的添加数据到数据库的函数,其测试函数如下:
public void testAdd() {
StudentDAO studentDAO = new StudentDAO(this.getContext());
Student student = new Student(1, "lily", 20);
studentDAO.add(student);
Log.i(TAG, "testAdd");
}
第一次测试成功,但再测试一次就失败了,其错误提示如下:android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1772)
at com.android.sqlite.dao.StudentDAO.add(StudentDAO.java:26)
at com.android.sqlite.activity.StudentDAOTest.testAdd(StudentDAOTest.java:16)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
找了半天也没发现自己的插入语句有哪里错误,最后才知道我在student表中把学生id设成了主键,第一次插入正确,当第二次测试时相当于再次插入同样的信息,由于id已存在,所以抛出了异常。
更多相关文章
- [Android]读写fb0测试
- padding和margin测试
- C语言的函数递归(下)
- ANDROID JNI的使用简介
- Android通信机制之Android架构(一)
- Android(安卓)学习笔记 Service (二) Remote Service Sample
- Android(安卓)NDK数据类型转换详解
- 修改android monkey源码使其支持UI自动化测试
- Android中getWidth和getMeasuredWidth的区别