getReadableDatabase以递归方式调用数据库

时间:2014-06-12 02:04:04

标签: android android-sqlite sqliteopenhelper

好的,所以我在这里讨论了所有问题,以及有关创建SQLiteDatabase的许多教程。昨晚我完成了这个工作,现在我得到了“递归调用数据库”错误,即使我回滚了我的更改。我需要有人来看看这段代码,看看他们是否能搞清楚。

助手班级:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_BACK);
    db.execSQL(CREATE_TABLE_FORWARD);
    db.execSQL(CREATE_TABLE_INWARD);
    db.execSQL(CREATE_TABLE_REVERSE);
    db.execSQL(CREATE_TABLE_TWIST);

    // call the methods to fill the dive table data
    fillForwardDives();
    fillBackDives();
    fillInwardDives();
    fillReverseDives();
    fillTwistDives();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_BACK);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_FORWARD);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_INWARD);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_REVERSE);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_TWIST);

    onCreate(db);       
}
// -------------getting all dive names for spinner--------------------------------------//
public List<String> getForwardNames(){
    List<String> diveNames = new ArrayList<String>();

    String selectQuery = "SELECT " + DIVE_NAME + " FROM " + TABLE_FORWARD;

    Log.e(LOG, selectQuery);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    if (c.moveToFirst()){
        do{
            ForwardDB f = new ForwardDB();
            diveNames.add(f.setDiveName(c.getString(c.getColumnIndex(DIVE_NAME))));             
        }while (c.moveToNext());
    }
    c.close();
    db.close();
    return diveNames;
}

这是在主要活动中调用它的方法:

// Load spinner data from the SQLite database
    private void loadSpinnerData(){     
    DatabaseHelper db = new DatabaseHelper(getApplicationContext());

    List<String> diveName = db.getForwardNames();

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, diveName);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);
}

从我读过的所有内容中我知道我应该在GetForwardNames方法中放置一个sqlite参数,如下所示:getForwardNames(SQLiteDatabase db)但是当我这样做时无法从主活动中正确调用它。

它在GetForwardNames方法中的这一行上死掉了:     SQLiteDatabase db = this.getReadableDatabase();

感谢您的光临。

调用堆栈和其他方法:

06-12 06:42:37.843: E/AndroidRuntime(735): Caused by: java.lang.IllegalStateException:        getDatabase called recursively
06-12 06:42:37.843: E/AndroidRuntime(735):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:204)
06-12 06:42:37.843: E/AndroidRuntime(735):  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
06-12 06:42:37.843: E/AndroidRuntime(735):  at info.sqlite.helper.DatabaseHelper.getForwardNames(DatabaseHelper.java:151)
06-12 06:42:37.843: E/AndroidRuntime(735):  at info.sqlite.helper.DatabaseHelper.fillForwardDives(DatabaseHelper.java:366)
06-12 06:42:37.843: E/AndroidRuntime(735):  at info.sqlite.helper.DatabaseHelper.onCreate(DatabaseHelper.java:98)

我的填充代码和创建填充db的方法:

private void fillForwardDives(SQLiteDatabase db) {
        ForwardDB forward1 = new ForwardDB(101, "Forward Dive", 1.4, 1.3, 1.2, 0.0, 1.6, 1.5, 1.4, 0.0);
        ForwardDB forward2 = new ForwardDB(102, "Forward Sommersault", 1.6, 1.5, 1.4, 0.0, 1.7, 1.6, 1.5, 0.0);
        ForwardDB forward3 = new ForwardDB(103, "Forward 1 1/2 Sommersaults", 0.0, 1.7, 1.6, 0.0, 1.9, 1.6, 1.5, 0.0);
        ForwardDB forward4 = new ForwardDB(104, "Forward Double Sommersaults", 0.0, 2.3, 2.2, 0.0, 0.0, 2.1, 2.0, 0.0);
        ForwardDB forward5 = new ForwardDB(105, "Forward 2 1/2 Sommersaults", 0.0, 2.6, 2.4, 0.0, 0.0, 2.4, 2.2, 0.0);
        ForwardDB forward6 = new ForwardDB(106, "Forward Triple Sommersaults", 0.0, 0.0, 2.9, 0.0, 0.0, 2.8, 2.5, 0.0);
        ForwardDB forward7 = new ForwardDB(107, "Forward 3 1/2 Sommersaults", 0.0, 0.0, 3.0, 0.0, 0.0, 3.1, 2.8, 0.0);
        ForwardDB forward8 = new ForwardDB(109, "Forward 4 1/2 Sommersaults", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.5, 0.0);
        ForwardDB forward9 = new ForwardDB(112, "Forward Flying Sommersault", 0.0, 1.7, 1.6, 0.0, 0.0, 1.8, 1.7, 0.0);
        ForwardDB forward10 = new ForwardDB(113, "Forward Flying 1 1/2    Sommersaults", 0.0, 1.9, 1.8, 0.0, 0.0, 1.8, 1.7, 0.0);
        ForwardDB forward11 = new ForwardDB(115, "Forward Flying 2 1/2 Sommersaults", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.5, 0.0);

        createForward(forward1, db);
        createForward(forward2, db);
        createForward(forward3, db);
        createForward(forward4, db);
        createForward(forward5, db);
        createForward(forward6, db);
        createForward(forward7, db);
        createForward(forward8, db);
        createForward(forward9, db);
        createForward(forward10, db);
        createForward(forward11, db);

        Log.d("Forward Count", "Forward Count: " + getForwardNames().size());   
        closeDB();
}

public void createForward(ForwardDB forward, SQLiteDatabase db) {
    //SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ID, forward.getId());
    values.put(DIVE_NAME, forward.getDiveName());
    values.put(ONE_A, forward.getOneA());
    values.put(ONE_B, forward.getOneB());
    values.put(ONE_C, forward.getOneC());
    values.put(ONE_D, forward.getOneD());
    values.put(THREE_A, forward.getThreeA());
    values.put(THREE_B, forward.getThreeB());
    values.put(THREE_C, forward.getThreeC());
    values.put(THREE_D, forward.getThreeD());

    db.insert(TABLE_FORWARD, null, values);             
}

1 个答案:

答案 0 :(得分:3)

您的fill...()方法可能会调用getWritableDatabase()。您调用fill...()onCreate()种方法,这些方法是通过调用getWritableDatabase()getReadableDatabase()触发的。这会导致递归:您在之前的调用尚未完成时调用get...Database()

使用传入的SQLiteDatabase作为onCreate()的参数。例如,

@Override
void onCreate(SQLiteDatabase db) {
    // ...
    fillForwardDives(db);
    //...
}

void fillForwardDives(SQLiteDatabase db) {
    // now use the passed-in db instead of calling getWritableDatabase()
}