0

Hi i have issue with database SQLITE android

please check my code for reference

when i am going to insert values it returns -1 that i come to know from debug which is not inserting values so tell me what is wrong
i am inserting value from list view.

public void add_device(String data,
        ArrayList<HashMap<String, String>> jsonlist) {
    try {
        SQLiteDatabase db = this.getReadableDatabase();
        // database = this.getWritableDatabase();
        for (HashMap<String, String> map : jsonlist) {

            ContentValues values = new ContentValues();
            values.put(SAVE_COLUMN_NAME, data);
            values.put(SAVE_COLUMN_KEY, map.get(SAVE_COLUMN_KEY));
            values.put(SAVE_COLUMN_VALUE, map.get(SAVE_COLUMN_VALUE));
            @SuppressWarnings("unused")
            Long int1 = db.insert(SAVE_TABLE_NAME, null, values);
            Log.i("insserted value ", int1 + "");
        }

    }
    /*
     * for(HashMap<String, String> map : mylist){ ContentValues cv = new
     * ContentValues(); cv.put(FILE_NAME, map.get(FILE_NAME)); cv.put(DESC,
     * map.get(DESC)); cv.put(UPLOADED_BY, map.get(DATE_UPLOADED));
     * cv.put(ACTION, map.get(FILE_NAME)); cv.put(ID, map.get(ID));
     * cv.put(FILE_URI, map.get(FILE_URI)); db.insert("tablename", null,
     * cv); }
     */

    catch (Exception e) {
        // TODO: handle exception
    }
}

thanks in advance

4
  • check logcat..will get hint may be Commented Dec 7, 2013 at 12:09
  • it show no such table exist so now how to create table ? Commented Dec 7, 2013 at 12:12
  • see my answer..if any query let me know Commented Dec 7, 2013 at 12:18
  • TODO: handle exception: do that! Commented Dec 7, 2013 at 17:12

3 Answers 3

2

I am guessing that the "-1" value is coming from your Log.i(...) message, so the -1 is the return value of the db.insert(...) call. The -1 indicates an error occurred.

Instead of using insert, user insertOrThrow(...) and look at the exception for clues as to why there is a problem.

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

1 Comment

public static final String CREATE_SAVE_TABLE = "CREATE TABLE" + " " + SAVE_TABLE_NAME + "(" + SAVE_COLUMN_ID + "INTEGER PRIMERY KEY AUTOINCREMENT," + SAVE_COLUMN_NAME + " TEXT," + SAVE_COLUMN_KEY + " TEXT," + SAVE_COLUMN_VALUE + " TEXT" + ");"; can you check it this is true query??
1

-1 is returned when you are inserting the records. This can be due to violation of table properties (like conflict between type of data you are inserting and the type of column in table ,is "key" and "Value" column in table are of string type) also there can be several other reasons for this like you may be missing any table column value in insert operation or value of 'SAVE_COLUMN_NAME','SAVE_COLUMN_KEY','SAVE_COLUMN_VALUE' doesn't matches with respective column names in table .

[while running app open separate command prompt and write adb logcat ) and show the result here (specifically when you try to insert the record) so that we have more information related to issue]

[EDIT]

OK so i think i have found the problem

 SQLiteDatabase db = this.getReadableDatabase();

and you are inserting statement

   Long int1 = db.insert(SAVE_TABLE_NAME, null, values);

INSERT SHOULD BE DONE USING WRITABLE DATABASE NOT READABLE DATABASE.

Here SQLiteDatabase db is a Readable Database it should be a Writable database

change

         SQLiteDatabase db = this.getReadableDatabase();

to

         SQLiteDatabase db = this.getWritableDatabase();

Comments

0

I have made changes like

public void add_device(String data,ArrayList<HashMap<String, String>> jsonlist) {
    try {
        database = this.getWritableDatabase();
        for (HashMap<String, String> map : jsonlist) {
            ContentValues values = new ContentValues();
            values.put(SAVE_COLUMN_NAME, data);
            values.put(SAVE_COLUMN_KEY, map.get(SAVE_COLUMN_KEY));
            values.put(SAVE_COLUMN_VALUE, map.get(SAVE_COLUMN_VALUE));
            Long int1 = database.insertOrThrow(SAVE_TABLE_NAME, null,
                    values);
            Log.i("insserted value ", int1 + "");
        }
    }

    /*
     * for(HashMap<String, String> map : mylist){ ContentValues cv = new
     * ContentValues(); cv.put(FILE_NAME, map.get(FILE_NAME)); cv.put(DESC,
     * map.get(DESC)); cv.put(UPLOADED_BY, map.get(DATE_UPLOADED));
     * cv.put(ACTION, map.get(FILE_NAME)); cv.put(ID, map.get(ID));
     * cv.put(FILE_URI, map.get(FILE_URI)); db.insert("tablename", null,
     * cv); }
     */
    catch (Exception e) {

    }
} 

Comments

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.