我从 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
请告诉我,我应该如何在类和数据库中组织正确的数据保存。