The data that is fetched from a server in json format. The data fetched is stored into arraylists. The arraylists are looped through and stored into the database. The data does get retrieved and stored into the arrayLists I have confirmed by printing them out. Whats weird is in the init method there is a getNumberOfEntries method which returns 26 BUT getNumberOfEntries returns 0 if I call it after calling init().
This is the method that loops through the arraylist
private void init()
{
db = dbHelper.getWritableDatabase();
dbHelper.dropTable(db);
db = dbHelper.getWritableDatabase();
dbHelper.createTable(db);
final SQLiteDatabase db;
final long numEntries;
db = dbHelper.getWritableDatabase();
numEntries = dbHelper.getNumberOfEntries(db);
if(numEntries == 0)
{
db.beginTransaction();
try
{
for (int i=0; i < courseNameArrayList.size(); i++)
{
dbHelper.insertCourses(db,
courseTermArrayList.get(i),
courseNumberArrayList.get(i),
courseNameArrayList.get(i),
courseDescriptionArrayList.get(i));
}
}
catch (Exception e)
{
Log.d(TAG, "init exception");
Log.d(TAG, e.getMessage());
}
finally
{
Log.d(TAG, "In Finally init");
final long numEntries2 = dbHelper.getNumberOfEntries(db);
Log.d(TAG, "init finally num entries: " + numEntries2); // HERE IT LOGS AS 26 ENTRIES
db.endTransaction();
}
}
db.close();
}
This is the insertCourses method that gets called. I believe the problem is in this code
public void insertCourses(final SQLiteDatabase db,
final String term,
final String courseNumber,
final String courseName,
final String courseDescription)
{
Log.d(TAG, "Inserting Courses" + " " + term + " " + courseNumber + " " + courseName +
" " + courseDescription);
final ContentValues contentValues;
contentValues = new ContentValues();
contentValues.put(COL_TERM, term);
contentValues.put(COL_COURSE_NUM, courseNumber);
contentValues.put(COL_COURSE_NAME, courseName);
contentValues.put(COL_COURSE_DESCRIPTION, courseDescription);
try
{
Log.d(TAG, "In insertCourses try");
db.insertOrThrow(TABLE_NAME, null, contentValues);
}
catch (Exception e)
{
Log.d(TAG, "In insertCourses catch");
String exception = e.getMessage();
Log.d(TAG, exception);
}
}
getNumberOfEntries method
public long getNumberOfEntries(final SQLiteDatabase db)
{
Log.d(TAG, "Get Number of Entries");
final long numEntries;
numEntries = DatabaseUtils.queryNumEntries(db, TABLE_NAME);
return (numEntries);
}
Table structure
private static final String CREATE_COURSE_INFO_TABLE = "CREATE TABLE IF NOT EXISTS " +
TABLE_NAME + " ( " +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_TERM + " TEXT NOT NULL, " +
COL_COURSE_NUM + " TEXT NOT NULL, " +
COL_COURSE_NAME + " TEXT NOT NULL, " +
COL_COURSE_DESCRIPTION + " TEXT NOT NULL)";