1

ERROR- am getting as type mistmatch. Here is my code-Please help

public void addDetailDescription(List<Detail> detailList) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    for (int i = 0; i < detailList.size(); i++) {
        Log.e("vlaue inserting==", "" + detailList.get(i));
        values.put(DETAIL_ID, String.valueOf(detailList.get(i)));
        values.put(COLUMN_HEADING, String.valueOf(detailList.get(i)));
        values.put(COLUMN_IMAGE_DETAIl, String.valueOf(detailList.get(i)));
        values.put(COLUMN_DETAIL, String.valueOf(detailList.get(i)));
        values.put(DETAIL_DESCRIPTION_ID, String.valueOf(detailList.get(i)));
    }
    db.insert(DESCRIPTION_DETAIL_TABLE, null, values);

    db.close();
}

1 Answer 1

1

I think you have to write a function like this and in a for cycle ,call this function for each item of list.

public long SaveUsers(Users users) {
    long id = -1;

    ContentValues contentValues = null;
    try {
        contentValues = new ContentValues();
        contentValues.put("name", users.getName());
        contentValues.put("email", users.getEmail());
        contentValues.put("phone", users.getPhone());
        contentValues.put("password", users.getPassword());

        db=sqliteHelper.getWritableDatabase();
        id = db.insert(SqliteHelper.TABLE_USERS, null, contentValues);

    } catch (Exception e) {
        Log.d("Database Insert","Exception:"+e.getMessage());
    } finally {
        if(db!=null && db.isOpen())
            db.close();
    }
    return id;
}

In your code, contentValue change every time and finally just the last item will be inserted to database and it's completely wrong.

Sign up to request clarification or add additional context in comments.

7 Comments

.Thank you! Its successfully stored in sqlite. But its showing a lastrow of MYSQL table after fetching data from JSON.Kindly help.its showing error as sqlite.SQLiteConstraintException: UNIQUE constraint failed
Here my JSON code-JSONObject product = array.getJSONObject(i); detail.setId(product.getInt("id")); detail.setHeading(product.getString("heading")); detail.setImage(product.getString("images")); detail.setDetaildesc(product.getString("detaildescription")); detail.setListid(product.getInt("listid"));detailList.add(detail);//metohd to call adapter// displayDomain(detail);
this error happens when you're trying to insert an already existing id. Since the key is UNIQUE, you can't insert. Please read your code again. detail.setId(product.getInt("id")) is your id and you have to define this field unigue in SQLite as primary key, and when I look on your first code , this line, values.put(DETAIL_ID, String.valueOf(detailList.get(i))); you just get a item from your list but it's not id ! may be you should write String.valueOf(detailList.get(i).getid)), this is depends to your list.
@Freeshteh Naji Hi thanks for your response.As you were mentioned i changed my code in to detailList.get(i).getid).but sill same error is coming .Only last row of MYSQL table is displaying (two times) in sqlite.As am beginner in android development.Is any other way please suggest to display two rows with same id fetch from MYSQl and store it in SQLITE,.
once uninstall app to clear SQLite Database, And run the app again. The best thing you can do is debugging, put break point where get Id from list and where insert Id into Database.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.