0

I have two tables, in the open application is going to check.

But when I create these two SQLite Dao objects there is always a table can not be created.

This table is CashPayDao, even if I changed the implementation of the order to amend and modify the database version number will not be created.

When I commented out WXPayDao will be able to successfully create CashPayDao corresponding table.

First, create two SQLite Dao objects in onCreate

    cashDao = CashPayDao(this)
    wxDao = WXPayDao(this)

CashPayDao content:

class CashPayDao(context: Context) {
    private val payHelper = CashPayDBHelper(context)

CashPayDBHelper content:

class CashPayDBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(SQLITE_CREATE)
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        db.execSQL(SQLITE_DELETE_ENTRIES)
        onCreate(db)
    }

    override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        onUpgrade(db, oldVersion, newVersion)
    }

    companion object {
        private val SQLITE_CREATE = "create table if not exists " + CASHPAY_TABLE_NAME + " (" +
                OUT_TRADE_NO + TEXT_TYPE + " PRIMARY KEY, " +
                TEL_SEQ + TEXT_TYPE + COMMA_SEP +
                TOTAL_FEE + DOUBLE_TYPE + COMMA_SEP +
                STORE_ID + TEXT_TYPE + COMMA_SEP +
                ASS_POS + INT_TYPE + COMMA_SEP +
                NEXT_TRANNO + INT_TYPE + COMMA_SEP +
                SEQ + TEXT_TYPE + COMMA_SEP +
                THE_STEP + INT_TYPE + COMMA_SEP +
                ERROR_MESSAGE + TEXT_TYPE + COMMA_SEP +
                IS_DONE + INT_TYPE + DEFAULT + COMMA_SEP +
                IS_UPLOAD + INT_TYPE + DEFAULT + COMMA_SEP +
                UPLOAD_COUNT + INT_TYPE + DEFAULT + COMMA_SEP +
                CREATE_TIME + " DATETIME DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')))"

        private val SQLITE_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + CASHPAY_TABLE_NAME
    }
}

WXPayDao content:

class WXPayDao(context: Context) {
    private val payHelper = WXPayDBHelper(context)

WXPayDBHelper content:

class WXPayDBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(SQLITE_CREATE)
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        db.execSQL(SQLITE_DELETE_ENTRIES)
        onCreate(db)
    }

    override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        onUpgrade(db, oldVersion, newVersion)
    }

    companion object {
        private val SQLITE_CREATE = "create table if not exists " + WXPAY_TABLE_NAME + " (" +
                OUT_TRADE_NO + TEXT_TYPE + " PRIMARY KEY, " +
                TRANSACTION_ID + TEXT_TYPE + COMMA_SEP +
                TEL_SEQ + TEXT_TYPE + COMMA_SEP +
                TOTAL_FEE + DOUBLE_TYPE + COMMA_SEP +
                STORE_ID + TEXT_TYPE + COMMA_SEP +
                ASS_POS + INT_TYPE + COMMA_SEP +
                NEXT_TRANNO + INT_TYPE + COMMA_SEP +
                SEQ + TEXT_TYPE + COMMA_SEP +
                OPENID + TEXT_TYPE + COMMA_SEP +
                COUPON_FEE + DOUBLE_TYPE + COMMA_SEP +
                THE_STEP + INT_TYPE + COMMA_SEP +
                ERROR_MESSAGE + TEXT_TYPE + COMMA_SEP +
                IS_DONE + INT_TYPE + DEFAULT + COMMA_SEP +
                IS_UPLOAD + INT_TYPE + DEFAULT + COMMA_SEP +
                UPLOAD_COUNT + INT_TYPE + DEFAULT + COMMA_SEP +
                CREATE_TIME + " DATETIME DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')))"

        private val SQLITE_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + WXPAY_TABLE_NAME
    }
}

I have carefully identified the table name is different, one is cash_pay, one is wx_pay, the database is the same, and now did not understand why this is so.

What should I do? Thank! (@•ᴗ•@)


I've now solved it by running both tables on Helper's onCreate and onUpgrade.

CashPayDBHelper content:

override fun onCreate(db: SQLiteDatabase) {
    db.execSQL(SQLITE_CREATE)
    db.execSQL(WXPayDBHelper.SQLITE_CREATE)
}

override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
    db.execSQL(SQLITE_DELETE_ENTRIES)
    db.execSQL(WXPayDBHelper.SQLITE_DELETE_ENTRIES)
    onCreate(db)
}

WXPayDBHelper content:

override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(SQLITE_CREATE)
        db.execSQL(CashPayDBHelper.SQLITE_CREATE)
    }

override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        db.execSQL(SQLITE_DELETE_ENTRIES)
        db.execSQL(CashPayDBHelper.SQLITE_DELETE_ENTRIES)
        onCreate(db)
    }

I have a bold idea......

I probably know the reason, but I think I now need to study the source code......

CrazyZhang
  • 45
  • 8
  • I think if I now work on two tables in Helper's onCreate and onUpgrade it will solve the problem, but I actually have another table that does the same thing, but the other table is normal, with no problem. The previous table and wx table no problem, when I joined the third table cash is broken, it is a problem. – CrazyZhang Dec 20 '17 at 08:56

1 Answers1

1

SQLiteOpenHelper manages database files, not individual tables. If you have more than one table in the same database, put the code to manage them in the same database helper.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • I understand, but I did not have a problem before, ah, this is very strange, I think I still need to understand the internal implementation, thank u~ – CrazyZhang Dec 20 '17 at 09:34
  • Have a look at https://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto Dec 20 '17 at 09:34