加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用fastjson导致主键自增列错误 Failed to insert row into con

发布时间:2020-12-16 19:18:20 所属栏目:百科 来源:网络整理
导读:数据库中id列是自增主键 使用fastjson将对象转为json,再将json转为ContentValues,由ContentProvider进行插入操作报错 android.database.SQLException: Failed to insert row into content://xxxxxxxxxxxxxxxxxxxxxxxxxxxx 发现是由于对象转json时,对象的id

数据库中id列是自增主键

使用fastjson将对象转为json,再将json转为ContentValues,由ContentProvider进行插入操作报错

android.database.SQLException: Failed to insert row into content://xxxxxxxxxxxxxxxxxxxxxxxxxxxx

发现是由于对象转json时,对象的id字段虽然没有值,但转json会默认生成带有id的key字段,实际插入数据库时的语句为

“insert into table xxx(id,xxx) values(null,xxx)”

解决方法:

转成json后去掉id字段

json.remove("id")

或者转成ContentValues后去掉id字段

cv.remove("id");

实际插入数据库时的语句变为
“insert into table xxx(xxx) values(xxx)”
自增字段id生效,插入成功

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读