当前位置:   article > 正文

KotLin ORM数据库_kotlin orm 生成table

kotlin orm 生成table

KotLin ORM 数据库

数据库实体类

@DatabaseTable(tableName = "t_menu") class MenuDb {


    @DatabaseField(generatedId = true) val id:Int = 0

    @DatabaseField(columnName = "menuname") var menuname:String ?= null

    @DatabaseField(columnName = "menunum") var menunum:String ?= null

    @DatabaseField(columnName = "pic") var pic:String ?= null

    @DatabaseField(columnName = "shapname") var shapname:String ?= null

    @DatabaseField(columnName = "address") var address:String ?= null
//
    @DatabaseField(columnName = "sum") var sum:Int = 0
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

OpenHolder类

class MenuOpenHelper(context: Context?) : OrmLiteSqliteOpenHelper(context,"menu.db",null,1) {

    override fun onCreate(database: SQLiteDatabase?, connectionSource: ConnectionSource?) {
       //创建user表

        TableUtils.createTable(connectionSource,MenuDb::class.java)
    }


    override fun onUpgrade(
        db: SQLiteDatabase?,
        connectionSource: ConnectionSource?,
        oldVersion: Int,
        newVersion: Int
    ) {

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

添加数据库

            val menuOpenHelper =MenuOpenHelper(this)
            dao = menuOpenHelper.getDao(MenuDb::class.java)
            val menuDb:MenuDb = MenuDb()

                    menuDb.menuname = menuname
                    menuDb.pic = pic
                    menuDb.menunum = num.toString()
                    menuDb.shapname = shapname
                    menuDb.address = address
                    menuDb.sum = 1


                    dao.create(menuDb)

                    Toast.makeText(this@MeauActivity,"加入成功",Toast.LENGTH_SHORT).show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号