Android sqlite数据未插入数据库

时间:2016-12-08 01:15:56

标签: android sqlite

以json格式从服务器获取的数据。获取的数据存储在arraylists中。 arraylists循环并存储到数据库中。数据确实被检索并存储到我通过打印出来确认的数组列表中。奇怪的是在init方法中有一个getNumberOfEntries方法,如果我在调用getNumberOfEntries后调用它,则返回26但init()返回0。

这是循环遍历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();
}

这是被调用的insertCourses方法。我相信问题在于此代码

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方法

public long getNumberOfEntries(final SQLiteDatabase db)
{
    Log.d(TAG, "Get Number of Entries");
    final long numEntries;

    numEntries = DatabaseUtils.queryNumEntries(db, TABLE_NAME);
    return (numEntries);
}

表格结构

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)";

1 个答案:

答案 0 :(得分:1)

第二次返回0,因为事务被回滚,然后没有插入行。

你应该使用这种模式。

   db.beginTransaction();
   try {
     ...
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }