Android SQLite错误打开数据库

时间:2016-12-30 13:57:06

标签: android database sqlite cursor

我正在使用SQLiteOpenHelper并且我的类完全正常工作,但我的一个方法中确实有一个错误,我从光标获取getCount()后没有关闭数据库。现在,每次我启动应用程序时,都会收到错误:

    java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:

我正在尝试关闭该连接,但我无法对该类执行任何方法,因为我总是遇到此错误。

我实际上重新创建了一个全新的Android应用并逐个复制了我的源文件,但仍然收到了同样的错误。

在我的MainActivity.java中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BillDatabaseHelper db = new BillDatabaseHelper(this);
    db.recreateDatabases(); // this is the line that causes the error

    viewPagerFragment = new ViewPagerFragment();
    transaction = manager.beginTransaction();
    transaction.add(R.id.placeholderMain, viewPagerFragment);
    transaction.commit();
}

在我的DatabaseHelper.java中:

public void recreateDatabases() {
    SQLiteDatabase db = this.getWritableDatabase();
    onCreate(db);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRANSACTIONS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_BILL_INFO);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_REMINDERS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_BILL_DATE_INFO);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PAYDATES);

    String strCreateTransactionsTableSQL =
            "CREATE TABLE " + TABLE_TRANSACTIONS + "("
            + KEY_TRAN_ID + " INTEGER PRIMARY KEY,"
            + KEY_TRAN_DESC + " TEXT,"
            + KEY_TRAN_AMOUNT + " REAL,"
            + KEY_TRAN_PURCHASE_DATE + " TEXT);";

    String strCreateBillInfoTableSQL =
            "CREATE TABLE " + TABLE_BILL_INFO + "("
            + KEY_BILL_ID + " INTEGER PRIMARY KEY,"
            + KEY_BILL_NAME + " TEXT,"
            + KEY_BILL_BALANCE + " FLOAT,"
            + KEY_BILL_DUE_DATE + " TEXT,"
            + KEY_BILL_MINIMUM + " FLOAT,"
            + KEY_BILL_CURRENT + " FLOAT);";

    String strCreateRemindersTableSQL =
            "CREATE TABLE " + TABLE_REMINDERS + "("
            + KEY_REM_ID + " INTEGER PRIMARY KEY,"
            + KEY_REM_NAME + " TEXT,"
            + KEY_REM_DESC + " TEXT,"
            + KEY_REM_LOCATION + " TEXT,"
            + KEY_REM_START + " TEXT);";

    String strCreateBillDateTableSQL =
            "CREATE TABLE " + TABLE_BILL_DATE_INFO + "("
            + KEY_BILLDATE_PK + " INTEGER PRIMARY KEY,"
            + KEY_BILLDATE_TOBILL_ID + " INTEGER,"
            + KEY_BILLDATE_BILLID + " INTEGER,"
            + KEY_BILLDATE_PAYDATE + " TEXT,"
            + KEY_BILLDATE_BILLDATE + " TEXT,"
            + KEY_BILLDATE_OWEDTODAY + " CHARACTER(1),"
            + KEY_BILLDATE_BALANCE + " FLOAT,"
            + KEY_BILLDATE_CURRENT + " FLOAT,"
            + "FOREIGN KEY(" + KEY_BILLDATE_PK + ") REFERENCES " + TABLE_BILL_INFO + "(" + KEY_BILL_ID + "));";

    String strCreatePaydatesSQL =
            "CREATE TABLE " + TABLE_PAYDATES + "("
            + KEY_PAYDATE_ID + " INTEGER PRIMARY KEY,"
            + KEY_PAYDATE_DATE + " TEXT);";

    db.execSQL(strCreateTransactionsTableSQL);
    db.execSQL(strCreateBillInfoTableSQL);
    db.execSQL(strCreateRemindersTableSQL);
    db.execSQL(strCreateBillDateTableSQL);
    db.execSQL(strCreatePaydatesSQL);
}

我应该在哪里关闭此连接?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您不必关闭数据库连接,请参阅:

Best place to close database connection

您可以像这样打开数据库:

public class YourDatabase extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "yourdatabase.db";
    private SQLiteDatabase database;

    public YourDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // call this function once
    public void open() throws SQLException {
        database = this.getWritableDatabase();
    }

    private void create(SQLiteDatabase database) {
        database.execSQL("CREATE TABLE contacts (TEXT `name`, TEXT `number`)");
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        create(database);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    }
}

只需拨打open()一次,然后您就可以使用database属性来编写或读取数据库,例如database.rawQuery(selectQuery, null);