zoukankan      html  css  js  c++  java
  • Unique constraint on single String column with GreenDao2

    转: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();
    }
  • 相关阅读:
    更改文件、图片名称思路,我们的名字在以后添加_2等待
    UIStepper使用的具体解释的控制
    ArcSDE10.1配置Oracle 监听器来使用SQL操作ST_Geometry(个人改动版)
    SDE ST_Geometry SQL st_intersects查询很慢的解决方法
    通过SQL直接插入、修改ArcGIS SDE空间表中的数据
    使用POI读写word docx文件
    Weblogic读不到Word文件
    POI写Word换行
    使用POI转换word doc文件
    POI生成Web版Word文件
  • 原文地址:https://www.cnblogs.com/liulipeng/p/3935919.html
Copyright © 2011-2022 走看看