Android. database. SQLite. sqliteconstraintexception: Error Code 19: constraint failed

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.