I get JsonArray from web service and parse it with Gson to ArrayList of corresponding class.
This is my class:
@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
}
In my Activity I parse it to the ArrayList:
Type listType = new TypeToken<List<Category>>() {}.getType();
List<Category> tasks = new ArrayList<Category>();
tasks = gson.fromJson(array.toString(), listType);
In result, I get ArrayList with my data and it saves correctly. Then, in my DatabaseHandler, which extends from OrmLiteSqliteOpenHelper I use method for saving ArrayList in the database:
public void saveCategories(List<Category> contacts) throws SQLException {
Dao<Category, Integer> daoContact=this.getDao(Category.class);
for (Category contact : contacts) {
daoContact.create(contact);
}
}
But when I call this method with my ArrayList I get:
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
Tell me please, how I should to orginize correct data saving in classes and database.
As your exception is trying to tell you, the error is happening because Sqlite is throwing the exception:
"error code 19: constraint failed". Here’s the limited documentation around Sqlite error codes:So there is some constraint that you have hit. If you used ORMLite to generate your schema then the only thing I can think of is that you are trying to insert a row that has a duplicate ID field.
Edit:
After going back and forth, it turns out that there were already objects in the database with the same ids. By querying for the ids before doing the
create(...)you can then make a decision about callingupdate(...)orcreate(...).When I do this however, I get a better exception:
Are you sure you are using an up-to-date version of the Xerial Sqlite driver? I’m using version 3.6.20.