android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

本文介绍了一个在Android开发过程中遇到的单元测试问题。测试添加数据到数据库的功能时,由于学生ID作为主键,在第二次测试时尝试插入相同的ID导致SQLiteConstraintException异常。文章详细解释了异常的原因,并提供了如何解决该问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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已存在,所以抛出了异常。

 

03-05 17:45:49.133 05864 05796 W NetworkScheduler: Error inserting flex_time=3536000 job_id=-1 period=7073000 source=16 requires_charging=0 preferred_network_type=1 target_class=com.google.android.gms.measurement.PackageMeasurementTaskService user_id=0 target_package=com.google.android.gms tag=Measurement.PackageMeasurementTaskService.UPLOAD_TASK_TAG task_type=0 required_idleness_state=0 service_kind=0 source_version=250832000 persistence_level=1 preferred_charging_state=1 required_network_type=0 runtime=1741176949126 retry_strategy={"maximum_backoff_seconds":{"3600":0},"initial_backoff_seconds":{"30":0},"retry_policy":{"0":0}} last_runtime=0 [CONTEXT service_id=218 ] android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: pending_ops.tag, pending_ops.target_class, pending_ops.target_package, pending_ops.user_id (code 2067 SQLITE_CONSTRAINT_UNIQUE) at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:988) at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:814) at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:89) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1981) at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1875) at dreu.d(:com.google.android.gms@250832035@25.08.32 (260400-731361394):727) at drcl.n(:com.google.android.gms@250832035@25.08.32 (260400-731361394):29) at drcl.u(:com.google.android.gms@250832035@25.08.32 (260400-731361394):364) at drcl.h(:com.google.android.gms@250832035@25.08.32 (260400-731361394):51) at dqxa.run(:com.google.android.gms@250832035@25.08.32 (260400-731361394):72) at atim.c(:com.google.android.gms@250832035@25.08.32 (260400-731361394):50) at atim.run(:com.google.android.gms@250832035@25.08.32 (260400-731361394):76) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644) at atnz.run(:com.google.android.gms@250832035@25.08.32 (260400-731361394):8) at java.lang.Thread.run(Thread.java:1012)什么意思
最新发布
03-14
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值