欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android Kotlin使用SQLite案例详解

程序员文章站 2022-04-09 11:46:26
kotlin使用sqlite首先确定我们的目标,sqlite只是一种工具,我们需要掌握就是增删改查就可以,我们真正需要动脑的还是项目中的业务逻辑。我这篇文章写得比较适合新手,没用过sqlite的同学。...

kotlin使用sqlite

首先确定我们的目标,sqlite只是一种工具,我们需要掌握就是增删改查就可以,我们真正需要动脑的还是项目中的业务逻辑。我这篇文章写得比较适合新手,没用过sqlite的同学。
前期准备工作
新建一个类mydatabasehelper继承自sqliteopenhelper,代码如下:

class mydatabasehelper(var context: context, name: string, version: int) :
    sqliteopenhelper(context, name, null, version) {
    public var createbook="create table book (" +
            "id integer primary key autoincrement," +
            "author text," +
            "price real," +
            "pages integer," +
            "name text)"

    override fun oncreate(db: sqlitedatabase?) {
//        下面这个todo 如果不注释掉的话就会报错。
//        todo("not implemented") //to change body of created functions use file | settings | file templates.
        db?.execsql(createbook)
        toast.maketext(context,"create successed",toast.length_long).show()
    }

    override fun onupgrade(db: sqlitedatabase?, oldversion: int, newversion: int) {
//        todo("not implemented") //to change body of created functions use file | settings | file templates.
        db?.execsql("drop table if exists book")
        oncreate(db)
    }
}

对数据进行操作
操作比较简单,下面直接看代码:
activity中

class mysqlite : appcompatactivity() {

    override fun oncreate(savedinstancestate: bundle?) {
        super.oncreate(savedinstancestate)
        setcontentview(r.layout.activity_my_sqlite)
        val dbhelper=mydatabasehelper(this,"bookstore.db",1)
        /**
         * 创建表
         */
        btncreatedatabase.setonclicklistener {
            dbhelper.writabledatabase
        }
        /**
         * 添加数据
         */
        btnadddata.setonclicklistener {
            val db=dbhelper.writabledatabase
            val values1=contentvalues().apply {
//                第一条数据
                put("name","the da vinci code")
                put("author","dan broen")
                put("pages",454)
                put("price",16.96)
            }
            db.insert("book",null,values1)
            val  values2=contentvalues().apply {
//                第二条数据
                put("name","the lost symbol")
                put("author","dan brown")
                put("pages",510)
                put("price",19.95)
            }
            db.insert("book",null,values2)
        }
        btnupdatedata.setonclicklistener {
            val db=dbhelper.writabledatabase
            val values=contentvalues()
            values.put("price",10.99)
            db.update("book",values,"name=?", arrayof("the da vinci code"))
        }
        btndeletedata.setonclicklistener {
            val db=dbhelper.writabledatabase
            db.delete("book","pages>?", arrayof("500"))
        }
        btnquerydata.setonclicklistener {
            val db=dbhelper.writabledatabase
//            查询book表中所有数据
//            这里获取到是cursor对象
            val cursor=db.query("book",null,null,null,null,null,null)
            if (cursor.movetofirst()){
                do {
                    val name=cursor.getstring(cursor.getcolumnindex("name"))
                    val author=cursor.getstring(cursor.getcolumnindex("author"))
                    val pages=cursor.getstring(cursor.getcolumnindex("pages"))
                    val price=cursor.getstring(cursor.getcolumnindex("price"))
                    log.d("mainactivity","book name is $name")
                    log.d("mainactivity","author is $author")
                    log.d("mainactivity","pages is $pages")
                    log.d("mainactivity","price is $price")
                }while (cursor.movetonext())
            }
            cursor.close()
        }
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".sqlite.mysqlite">

    <button
        android:id="@+id/btncreatedatabase"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="createdatabase"
        android:textallcaps="false"
        app:layout_constrainttop_totopof="parent" />

    <button
        android:id="@+id/btnadddata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="adddata"
        android:textallcaps="false"
        app:layout_constrainttop_tobottomof="@+id/btncreatedatabase" />

    <button
        android:id="@+id/btnupdatedata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="updatedata"
        android:textallcaps="false"
        app:layout_constrainttop_tobottomof="@+id/btnadddata" />

    <button
        android:id="@+id/btndeletedata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="deletedata"
        app:layout_constrainttop_tobottomof="@+id/btnupdatedata" />

    <button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnquerydata"
        android:text="query data"
        android:textallcaps="false"
        app:layout_constrainttop_tobottomof="@+id/btndeletedata"/>
</androidx.constraintlayout.widget.constraintlayout>

到此这篇关于android kotlin使用sqlite案例详解的文章就介绍到这了,更多相关android kotlin使用sqlite内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!