0

我从 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
}

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

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

结果,我得到了带有我的数据的 ArrayList 并正确保存。然后,在从 OrmLiteSqliteOpenHelper 扩展的 DatabaseHandler 中,我使用将 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

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

4

2 回答 2

2

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

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

编辑:

来来回回,原来数据库中已经有相同id的对象了。通过在执行之前查询 id ,create(...)您可以决定是否调用update(...)or 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 版本。

于 2012-07-30T14:41:01.543 回答
0

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

于 2012-08-14T08:02:03.390 回答