Android SQLite数据库使用

Android SQLlite数据库

SQLlite数据库增删改查
创建一个帮助类继承自SQLiteOpenHelper类
在该类中定义创建表的sql语句,在onCreate()中调用SQLiteDatabase对象的execSQL()方法执行该语句

package com.example.sqllitedemo

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.widget.Toast

class MyDatabaseHelper(val context:Context,val name: String,version:Int):
        SQLiteOpenHelper(context,name,null,version) {
	//定义创建表的sql语句
    private val createBook = "create table Book(" +
            "id integer primary key autoincrement," +
            "author text," +
            "prise real," +
            "pages integer," +
            "name text)"

    private val createCategory = "create table Category (" +
            "id integer primary key autoincrement," +
            "category_name text," +
            "category_code integer)"

    override fun onCreate(db: SQLiteDatabase?) {
        db?.execSQL(createBook)
        db?.execSQL(createCategory)
        Toast.makeText(context,"数据库创建成功",Toast.LENGTH_SHORT).show()
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        if (oldVersion <=1 ){
            db?.execSQL(createCategory)
        }
    }
}

开始创建数据库以及对数据库进行增删改查

package com.example.sqllitedemo

import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val dbHelper by lazy {
        MyDatabaseHelper(this,"BookStore.db",1)
    }

    private lateinit var db: SQLiteDatabase

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initListener()
    }

    private fun initListener() {
        //创建数据库
        newDatabase.setOnClickListener {
            db = dbHelper.writableDatabase

        }
        //添加数据
        addData.setOnClickListener {
            db = dbHelper.writableDatabase
            val value = ContentValues().apply {
                put("name","第一行代码")
                put("author","郭霖")
                put("pages",692)
                put("prise",80.00)
            }
            db.insert("Book",null,value)
        }
        //更新数据
        updateData.setOnClickListener {
            db = dbHelper.writableDatabase
            val value = ContentValues()
            value.put("prise",78.90)
            db.update("Book",value,"name = ?", arrayOf("第一行代码"))
        }
        //删除数据
        deleteData.setOnClickListener {
            db = dbHelper.writableDatabase
            db.delete("Book","pages > ?", arrayOf("500"))
        }
        //查询数据
        selectData.setOnClickListener {
            //查询Book表中的所有数据
            db = dbHelper.writableDatabase
            val cursor = db.query("Book",null,null,null,null,null,null)
            if(cursor.moveToFirst()){
                do{
                    cursor.apply {
                        val name = getString(cursor.getColumnIndex("name"))
                        val author = getString(cursor.getColumnIndex("author"))
                        val pages = getInt(cursor.getColumnIndex("pages"))
                        val prise = getDouble(cursor.getColumnIndex("prise"))
                        println("-------书名:$name;作者:$author;页数:$pages;价格:$prise")
                    }
                }while (cursor.moveToNext())
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值