Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:10:35+00:00 2026-06-08T22:10:35+00:00

I get JsonArray from web service and parse it with Gson to ArrayList of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:10:37+00:00Added an answer on June 8, 2026 at 10:10 pm

    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 calling update(...) or create(...).

    When I do this however, I get a better exception:

    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)
    

    Are you sure you are using an up-to-date version of the Xerial Sqlite driver? I’m using version 3.6.20.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Get class from div inside an li and add to the same li. The
Here is my class where I try to get into a JSONArray some data
I need to get some information from an JSONArray and i thought tokener would
I'm trying to parse a JSONArray received from php. I see from my log
Im getting my data from a web service and loads to a list view.
I fetch a JSON array from a web service with touchJSON. Which looks like
I use asynctask to get data from web services. But progessdialog not show when
I am getting JSON data from a web service and would like to display
When I try to convert an HTTP POST response to JSONArray I get the
Previously i have used restful wcf webservices to get the data from server.But now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.