1

I am trying to insert the values from an array into SQLite database. The problem is that the function can insert only null values even though the array does not contain such values.

The function for insert :

public void addArrayEntry(String [] response){
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        //System.out.println(response.length);
        for (int i=1; i<response.length; i++){
            values.put(temperature,response[i]);
            db.insert(tableStatistics,null,values);
        }

        db.close();
    }

    catch (Exception e){
        Log.e("Error in insert", e.toString());
    }

}

String createTable = "CREATE TABLE statistics ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "temperature REAL, "+
            "light INTEGER, "+
            "humidity INTEGER)";

Array values :

05-08 14:24:21.405 10720-10720/org.security.andreiism.iotsecurity I/System.out﹕ 23.798828125
05-08 14:24:21.405 10720-10720/org.security.andreiism.iotsecurity I/System.out﹕ 744
05-08 14:24:21.405 10720-10720/org.security.andreiism.iotsecurity I/System.out﹕ 424
3
  • here it looks fine - maybe problem is elsewhere? or try debug to find what exactly is given to db.insert in values
    – rzysia
    May 8, 2015 at 11:56
  • I changed the for sequence to start from 0 and the type of the "temperature" field to "TEXT" and the output is the same.
    – andcsie
    May 8, 2015 at 12:03
  • Try moving ContentValues values = new ContentValues(); into the for loop
    – Alan S
    May 8, 2015 at 12:29

3 Answers 3

0

Try with below changes...

public void addArrayEntry(String [] response){
try {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    //System.out.println(response.length);
    for (int i=0; i<response.length; i++) {
        values.put(temperature,response[i]);            
    }

    db.insert(tableStatistics,null,values);
    db.close();
}

catch (Exception e){
    Log.e("Error in insert", e.toString());
}

}

String createTable = "CREATE TABLE statistics ( " +
        "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
        "temperature REAL, "+
        "light INTEGER, "+
        "humidity INTEGER)";
1
  • I tried with the replaced function, but the output is the same.
    – andcsie
    May 8, 2015 at 12:29
0

write your db.insert outside of the for loop. So all the values get only inserted once. What you do is inserting values at the first time with the value lets say 1. On the second time of the insert you insert 1 and 2. On the third time you insert 1, 2 and 3. Now your table will look like this:

id| temperature
1 |1
2 |1
3 |2
4 |1
5 |2
6 |3

this is because you defined values outside of the for loop and add values to it like to an ArrayList. But this won't get deleted till the next running trough the loop. So the new value will just be added to the values before, and all values together will be added to the list.

So your method should look like this:

public void addArrayEntry(String [] response){
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        //System.out.println(response.length);
        for (int i=1; i<response.length; i++){
            values.put(temperature,response[i]);

        }
        db.insert(tableStatistics,null,values);
        db.close();
    }

    catch (Exception e){
        Log.e("Error in insert", e.toString());
    }

}
0

Thank you all for the help. I finally succeded to insert the required values using the function :

public void addArrayEntry(String [] response) {
    try {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(temperature, response[1]);
        values.put(light, response[2]);
        values.put(humidity, response[3]);
        db.insert(tableStatistics, null, values);
        db.close();
    } catch (Exception e) {
        Log.e("Error in insert", e.toString());
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.