没有这样的表:TABLE_TEAM(代码1):,而在编译时:SELECT * FROM TABLE_TEAM WHERE(TEAM_ID ='133604')

时间:2018-11-26 09:47:05

标签: android database sqlite

我在android kotlin中遇到数据库问题。每当我尝试使用它时,总是说没有这样的桌子。但是我已经摆好桌子了。

这是我的数据库打开助手

    class MyDatabaseOpenHelper(ctx: Context) : ManagedSQLiteOpenHelper(ctx, "FavoriteTeam.db", null, 1) {
    companion object {
        private var instance: MyDatabaseOpenHelper? = null

        @Synchronized
        fun getInstance(ctx: Context): MyDatabaseOpenHelper {
            if (instance == null) {
                instance = MyDatabaseOpenHelper(ctx.applicationContext)
            }
            return instance as MyDatabaseOpenHelper
        }
    }

    override fun onCreate(db: SQLiteDatabase?) {
        // Here you create tables

        db?.createTable(FavoriteTeam.TABLE_TEAM, true,
                FavoriteTeam.ID to INTEGER + PRIMARY_KEY + AUTOINCREMENT,
                FavoriteTeam.TEAM_ID to TEXT + UNIQUE,
                FavoriteTeam.TEAM_NAME to TEXT,
                FavoriteTeam.TEAM_BADGE to TEXT)

        db?.createTable(Favorite.TABLE_FAVORITE, true,
                Favorite.ID to INTEGER + PRIMARY_KEY + AUTOINCREMENT,
                Favorite.EVENT_ID to TEXT + UNIQUE,
                Favorite.HOME_ID to TEXT + UNIQUE,
                Favorite.AWAY_ID to TEXT + UNIQUE,
                Favorite.TEAM_HOME to TEXT,
                Favorite.TEAM_AWAY to TEXT,
                Favorite.TEAM_HOME_SCORE to TEXT,
                Favorite.TEAM_AWAY_SCORE to TEXT,
                Favorite.DATE to TEXT)
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        // Here you can upgrade tables, as usual
        db.dropTable(Favorite.TABLE_FAVORITE, true)
        db.dropTable(FavoriteTeam.TABLE_TEAM, true)
    }
}

// Access property for Context
val Context.database: MyDatabaseOpenHelper
    get() = MyDatabaseOpenHelper.getInstance(applicationContext)

这是我的数据库数据

data class FavoriteTeam(val id: Long?, val teamId: String?, val teamName: String?, val teamBadge: String?) {

companion object {
    const val TABLE_TEAM: String = "TABLE_TEAM"
    const val ID: String = "ID_"
    const val TEAM_ID: String = "TEAM_ID"
    const val TEAM_NAME: String = "TEAM_NAME"
    const val TEAM_BADGE: String = "TEAM_BADGE"
}

}

这就是我所说的

private fun favoriteState() {
    database.use {
        val result = select(FavoriteTeam.TABLE_TEAM)
                .whereArgs("(TEAM_ID = {id})",
                        "id" to idTeam)
        val favorite = result.parseList(classParser<FavoriteTeam>())
        if (!favorite.isEmpty()) isFavorite = true
    }
}

,它总是显示此错误 no such table: TABLE_TEAM (code 1): , while compiling: SELECT * FROM TABLE_TEAM WHERE (TEAM_ID = '133604')

我不知道为什么。当我想创建表TABLE_FAVORITE时,效果很好,但是当我想创建TABLE_TEAM时,它总是显示出我要插入或选择的错误。如果您知道的话,请帮助我。谢谢

2 个答案:

答案 0 :(得分:1)

尝试卸载应用程序,然后再试一次,因为在OnCreate中完成了TABLE创建,在整个应用程序的生命周期中仅调用一次,因此新创建的TABLE无法反映它是旧的apk生成还是重新创建表将其删除后在onUpgrade中

答案 1 :(得分:0)

OnCreate在运行项目时基本上不会创建表,因此,删除表并运行项目。