Android数据库GreenDAO3.2.2的使用(五,SQLiteConstraintException因为主键报错)
来源:互联网 发布:朝鲜生活 知乎 编辑:程序博客网 时间:2024/06/05 08:18
GreenDao第一篇文章提到主键ID数据类型有坑,下面先看一下一次log:android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: USER._id (code 1555)
insert的时候id出现重复,但是设置主键了啊,为什么会重复呢?看代码:
@Entitypublic class User { @Id private long id;再进入Id看源码:
/** * Marks field is the primary key of the entity's table */@Retention(RetentionPolicy.SOURCE)@Target(ElementType.FIELD)public @interface Id { /** * Specifies that id should be auto-incremented (works only for Long/long fields) * Autoincrement on SQLite introduces additional resources usage and usually can be avoided * @see <a href="https://www.sqlite.org/autoinc.html">SQLite documentation</a> */ boolean autoincrement() default false;}好像也没有发现什么错啊,完后改为:
@Entitypublic class User { @Id(autoincrement = true) private long id;或者改为:
@Entitypublic class User { @Id private Long id;在insert的时候就不会报错了。
总结:在设置主键id为“long”的时候,加上(autoincrement=true),而使用“long”的包装类“Long”的时候,加上(autoincrement=true)或者不加都不会报错,具体什么原因导致的还没有弄清楚,欢迎指点。
阅读全文
1 0
- Android数据库GreenDAO3.2.2的使用(五,SQLiteConstraintException因为主键报错)
- Android数据库GreenDAO3.2.2的使用(二,数据库使用)
- Android数据库GreenDAO3.2.2的使用(一,集成数据库)
- Android数据库GreenDAO3.2.2的使用(四、数据库升级)
- Android数据库GreenDAO3.2.2的使用(三,GreenDao操作外部数据库----操作.db数据库)
- Android数据库——GreenDAO3.2.2的使用
- Android 数据库(4)-----GreenDao3
- Android GreenDao3.2.2简单使用
- android GreenDAO3.2.2简单使用
- greenDAO的使用详解---(2)greenDAO3.2.2数据库的升级
- android stdudio 数据库框架greendao3.2使用
- GreenDao3.2.2集成使用以及数据库的升级
- 使用FinalDb操作数据库出现SqliteConstraintException异常
- Android框架之路——GreenDao3.2.2的使用
- Android ORM框架 GreenDao3.0的使用
- Android 笔记:GreenDao3.2的使用,爱不释手
- Android Greendao3.0框架的使用
- Android ORM框架 GreenDao3.0的使用
- 环形缓冲区的设计及其在生产者消费者模式下的使用(并发有锁环形队列)
- 使用Dhtml和poi导出excle表格出现Error Type:LoadXMl Incorrect Json错误
- java的IO之File类
- 一个基于Python的shell自动化框架ShutIt
- 二、集合之LinkedList
- Android数据库GreenDAO3.2.2的使用(五,SQLiteConstraintException因为主键报错)
- Android中抓取妹子图网页数据并展示给界面
- RabbitMQ 集群之镜像同步
- Eclipse中服务器发布时发现项目名后出现括号,括号中名字与项目名不一致的问题
- USB Type-C到底是什么
- android 利用广播监听usb连接状态
- Codeforces Round #420 (Div. 2) B. Okabe and Banana Trees
- 详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]
- 最新csdn有奖任务答案