1

I am trying to add a new row inside an existing SQLite database, but for some reason it doesn't seem to do it. I've created this method to insert the row into the table:

public void insertToTable(ArrayList<String> courseAttributes, ArrayList<String> attributeValues){
  ContentValues cv = new ContentValues();
  for (int i = 0 ; i < courseAttributes.size() ; i++){
     cv.put(courseAttributes.get(i), attributeValues.get(i));
  }
  coursesDataBase.insert("courses", null, cv);
}

where courseAttributes is an arraylist of all the columns in the table, attributeValues is an arraylist of the corresponding values for the row I am trying to add, coursesDataBase is the SQLiteDatabase and "courses" is the name of the table in this database. I don't get any errors when I run it on the debugger, however it does not store the new row in the database. I call the method from another class, in between the following open and close methods:

public void openDataBase() throws SQLException{

 //Open the database
    String myPath = DB_PATH + DB_NAME;
 coursesDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

}

and

@Override
public synchronized void close() {

     if(coursesDataBase != null)
      coursesDataBase.close();

     super.close();

}
Peter
  • 47
  • 2
  • 8
  • 1
    Is insert() returning -1? If so then an error is occurring. Perhaps the table name is "course" instead of "courses". – Biff MaGriff Dec 13 '10 at 22:15
  • 1
    Make sure your DB_PATH + DB_NAME provide the correct full path to your database. Step though your code and ensure coursesDataBase is not null. SQLException may not be logged but may still be thrown - check the variables when stepping through your code. – John J Smith Dec 13 '10 at 23:52
  • Biff, I've double-checked the name of the course as I've actually had the problem of using a wrong table name before, but I am sure the name of the table is correct this time. John, the DB_PATH + DB_NAME does provide the correct full path to the database. After many hours it turned out it was actually working, but instead, the update method was not working correctly, so the program was returning an empty table instead of the populated table. Thanks for your help anyway, those were some really useful tips. – Peter Dec 14 '10 at 09:15
  • So What ended up being wrong and how did you fix it? I'm getting the same problem and I'm thinking of doing a raw query instead of using db.insert() – tricknology May 01 '13 at 10:15

0 Answers0