During the unit test on Android, the function of adding data to the database is tested. The test function is as follows:
Public void testadd (){
Studentdao = new studentdao (this. getcontext ());
Student = new student (1, "Lily", 20 );
Studentdao. Add (student );
Log. I (TAG, "testadd ");
}
The test was successful for the first time, but failed again. The error message is as follows:
Android. database. SQLite. sqliteconstraintexception: Error Code 19: constraint failed
At Android. database. SQLite. sqlitestatement. native_execute (native method)
At android.database.sqlite.sqlitestatement.exe cute (sqlitestatement. Java: 55)
At android.database.sqlite.sqlitedatabase.exe csql (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)
After searching for half a day, I didn't find any errors in my insert statement. I finally realized that I set the student ID to the primary key in the student table. The first insertion was correct, during the second test, the same information is inserted again. An exception is thrown because the ID already exists.