带有2个字段的ArrayList到ORMLite android中

时间:2012-07-30 13:53:03

标签: android arraylist ormlite

我从Web服务获取JsonArray并使用Gson将其解析为相应类的ArrayList。 这是我的班级:

@DatabaseTable(tableName = "categories")
public class Category {
    public final static String CATEGORY_TITLE_FIELD_NAME  = "title";
    @SerializedName("id")
    @DatabaseField(id = true)
    int id;
    @SerializedName("title")
    @DatabaseField(dataType = DataType.STRING, columnName = CATEGORY_TITLE_FIELD_NAME)
    String title;

    // need for ORMlite
    public Category() {}

    public Category (int id, String title) {
        this.id = id;
        this.title = title;
    }

    // getters/setters/toString methods here
}

在我的Activity中,我将其解析为ArrayList:

Type listType = new TypeToken<List<Category>>() {}.getType();
List<Category> tasks = new ArrayList<Category>();
tasks = gson.fromJson(array.toString(), listType);

结果,我得到了带有我的数据的ArrayList,它可以正确保存。然后,在我的DatabaseHandler中,它从OrmLiteSqliteOpenHelper扩展,我使用方法在数据库中保存ArrayList:

  public void saveCategories(List<Category> contacts) throws SQLException {
        Dao<Category, Integer> daoContact=this.getDao(Category.class);
        for (Category contact : contacts) {
            daoContact.create(contact);
        }
    }

但是当我用ArrayList调用这个方法时,我得到:

java.sql.SQLException: Unable to run insert stmt on object {title=Non-categorized id=1}: INSERT INTO `categories` (`title` ,`id` ) VALUES (?,?)
    at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
    at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:124)
    at com.j256.ormlite.stmt.StatementExecutor.create(StatementExecutor.java:363)
    at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:306)
    at com.library.DatabaseHandler.saveCategories(DatabaseHandler.java:148)
    at com.assignmentexpert.LoginActivity$1.onClick(LoginActivity.java:104)
    at android.view.View.performClick(View.java:2485)
    at android.view.View$PerformClick.run(View.java:9080)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.sql.SQLException: inserting to database failed: INSERT INTO `categories` (`title` ,`id` ) VALUES (?,?)
    at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
    at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:134)
    at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:89)
    ... 15 more
Caused by: android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:88)
    at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:122)
    ... 16 more

请告诉我,我应该如何在类和数据库中保存正确的数据保存。

2 个答案:

答案 0 :(得分:2)

正如您的异常试图告诉您的那样,错误正在发生,因为Sqlite抛出异常:"error code 19: constraint failed"。以下是有关Sqlite error codes的有限文档:

所以你遇到了一些限制。如果您使用ORMLite生成架构,那么我唯一能想到的就是您尝试插入一个具有重复ID字段的行。

修改

在来回反复之后,事实证明数据库中已经存在具有相同ID的对象。通过在执行create(...)之前查询ids ,您可以做出关于调用update(...)create(...)的决定。

但是,当我这样做时,我得到一个更好的例外:

java.sql.SQLException: Unable to run insert stmt on object
    com.j256.ormlite.db.SqliteConnectTest$IdConstraint@7971f189:
    INSERT INTO `idconstraint` (`id` ,`stuff` ) VALUES (?,?)
...
Caused by: java.sql.SQLException: Unable to run insert stmt on object
    com.j256.ormlite.db.SqliteConnectTest$IdConstraint@7971f189:
    INSERT INTO `idconstraint` (`id` ,`stuff` ) VALUES (?,?)
...
Caused by: java.sql.SQLException: [SQLITE_CONSTRAINT]  Abort due to
    constraint violation (PRIMARY KEY must be unique)

您确定使用的是最新版本的Xerial Sqlite驱动程序吗?我使用的是版本3.6.20。

答案 1 :(得分:0)

问题是我向数据库添加了两次元素。感谢Gray