转:http://software.techassistbox.com/unique-constraint-on-single-string-column-with-greendao_384521.html

I want to make unique constraint on String value with GreenDao. I have following piece of code:

Entity category = schema.addEntity('Category');
category.addIdProperty();
category.addStringProperty('Name').notNull();

How can I make sure that Name will be unique?

My tries:

I have found methods PropertyBuilder.unique() and PropertyBuilder.index() but none of this works.

index() doesn't work at all and allows to make duplication. unique() throws exception in Robolectric test but don't do anything when app runs on device Robolectric exception:

android.database.sqlite.SQLiteException: Cannot execute for last inserted row ID, base error code: 19
at org.robolectric.shadows.ShadowSQLiteConnection.rethrow(ShadowSQLiteConnection.java:48)
at org.robolectric.shadows.ShadowSQLiteConnection.access$500(ShadowSQLiteConnection.java:26)
at org.robolectric.shadows.ShadowSQLiteConnection$Connections.execute(ShadowSQLiteConnection.java:436)
at org.robolectric.shadows.ShadowSQLiteConnection.nativeExecuteForLastInsertedRowId(ShadowSQLiteConnection.java:239)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at de.greenrobot.dao.AbstractDao.executeInsert(AbstractDao.java:348)
at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293)

So far I have made something like this but I don't think it's the best solution.

However if this correct, is it good idea to add index() to name Property to get better performance?

public Long insert(Category category) {
if (getByName(category.getName()) != null) {
throw new DuplicateCategoryNameException(category.getName());
}
...
} public Category getByName(String name) {
return categoryDao.queryBuilder().where(CategoryDao.Properties.Name.eq(name)).build().unique();
}

Unique constraint on single String column with GreenDao2的更多相关文章

  1. Unique constraint on single String column with GreenDao

    转:http://stackoverflow.com/questions/22070281/greendao-support-for-unique-constraint-on-multiple-col ...

  2. SQL UNIQUE Constraint

    SQL UNIQUE Constraint The UNIQUE constraint uniquely identifies each record in a database table. The ...

  3. ORA-00001: unique constraint (string.string) violated 违反唯一约束条件(.)

    ORA-00001: unique constraint (string.string) violated   ORA-00001: 违反唯一约束条件(.) Cause: An UPDATE or I ...

  4. Oracle 唯一 约束(unique constraint) 与 索引(index) 关系说明

    一. 官网对Unique Constraints说明 http://download.oracle.com/docs/cd/E11882_01/server.112/e16508/datainte.h ...

  5. Sqlite - constraint failed[0x1555]: UNIQUE constraint failed

    执行插入操作时,出现异常constraint failed[0x1555]: UNIQUE constraint failed 意思是:sqlite 唯一约束失败 定位于某个表字段上,该字段是表的主键 ...

  6. ORA-00001: unique constraint (...) violated解决方案

    ORA-00001: unique constraint (...) violated 的解决方案 今天往Oracle数据库里插入数据一条记录的时候,报错了, 控制台抛出异常:违反唯一性约定, 我以为 ...

  7. sqlite3, IntegrityError: UNIQUE constraint failed when inserting a value

    sqlite报错: sqlite3.IntegrityError: UNIQUE constraint failed: IMAGESTATUE.ID 解决方案: Change INSERT to IN ...

  8. ORA-00001: unique constraint (...) violated并不一定是数据冲突

    原文链接:http://blog.163.com/jet_it_life/blog/static/205097083201301410303931/ 收到一位测试人员RAISE的JIRA,说在某张表上 ...

  9. Oracle之唯一性约束(UNIQUE Constraint)使用方法具体解释

    Oracle | PL/SQL唯一索引(Unique Constraint)使用方法 1 目标 用演示样例演示怎样创建.删除.禁用和使用唯一性约束. 2 什么是唯一性约束? 唯一性约束指表中一个字段或 ...

随机推荐

  1. 【LeetCode】Binary Tree Upside Down

    Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...

  2. Win10企业版转专业版

    原文地址:https://jingyan.baidu.com/article/86112f136624322737978797.html 转换ISO镜像下载地址: ed2k://|file|cn_wi ...

  3. 【C语言】字符串常量与指针

  4. 字符集不同导致的ORA-00972

    使用ssh登录RAC的两个节点,分别执行相同的sql语句,发现其中一个报错ora-00972: 检查后发现,左侧节点的客户端使用的字符集是SecureCRT的默认字符集,右侧使用的是UTF-8

  5. Loader Lock引起的一个Bug

    在Windows中,让程序模块化实现的一种方式,就是让事实上现为动态链接库. 然后在主程序启动的时候隐式或者显示的去载入动态链接库.可是假设不恰当的编写动态链接库的DllMain函数,将会引起意想不到 ...

  6. 温故而知新 phpstudy 设置 nginx 代理

    nginx.conif 找到 server 关键字配置 server { listen ; server_name localhost; #charset koi8-r; #access_log lo ...

  7. linux(centos6)搭建ftp服务器 -摘自网络

    前提 ssh服务已经开启,关闭防火墙,主机和虚拟机能ping通 查看ssh和防火墙的状态 service sshd status service iptables status 开启ssh服务 ser ...

  8. supervisor 完整安装步骤

    Supervisorhttp://www.jianshu.com/p/bf2b3f4dec73http://www.jianshu.com/p/9abffc905645http://blog.csdn ...

  9. redis 3.2.3的源码安装

    Install necessary packages On CentOS : yum install wget make gcc tcl On CentOS yum install wget make ...

  10. 查询系统负载信息 Linux 命令详解

    linux uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令过去只显示系统运行多久.现在,可以显示系统已经运行了多长 时间,信息显示依次为:现在时间.系统已经运 ...