SQLite数据库的基本使用与笔记

SQLite数据库的使用

1.创建数据库

class MyDatabaseHelper(val context : Context, name : String, version : Int) : 
		SQLiteOpenHelper(context, name, null, version) {
	private val createBook = "create table Book("+
			" id integer primary key autoincrement,"+
			"author text,"+
			"price real,"+
			"pages integer,"+
			"name text)"
			//"create table Book( 	id iteger primary key autoincrement, 
			//						author text, //字符串
			//					 	prive real,  //浮点
			//					 	pages integer  //整型
			//					 	name text)"	//字符串-
	override fun onCreate(db: SQLiteDatabase){
		db.execSQL(createBook)
	}

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

}

我们把建表语句定义成一个字符串变量,然后再onCreate()中调用execSQL()去执行这条建表语句。

class MainActivity : APPCompatActivity(){
	override fun onCreate(savedInstanceState : Bundle?){
		...
		val dbHelper = MyDatabaseHelper(this, "BookStore.db", 1)
		btn.setOnClickListener{ 
			dbHelper.writableDatabase // 这里getWritableDatabase 
		}
	}
}

getWritableDatabase() / getReadableDatabase() :
若存在则返回databse
不存在则执行onCreate()

2.升级数据库(增加表)

若现在要增加这个表Category

String create_str = "create table Category(
	id integer primary key autoincrement,
	category_name text,
	category_code integer )"

但在MyDatabaseHelper类里要加上相应代码

class MyDatabaseHelper(val context : Context, name : String, version : Int) : 
		SQLiteOpenHelper(context, name, null, version) {
	private val createBook = "create table Book("+
			" id integer primary key autoincrement,"+
			"author text,"+
			"price 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)
	}

	override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int){
		//若存在则直接清除
		db.execSQL("drop table if exists Book")
		db.execSQL("drop table if exists Category")
		//手动显式调用onCreate()重新创建表
		db.onCreate()
	}									 	

}

在MainActivity中指定新的版本号才会回调onUpgrade()

class MainActivity : APPCompatActivity(){
	override fun onCreate(savedInstanceState : Bundle?){
		...
		val dbHelper = MyDatabaseHelper(this, "BookStore.db", 2)  //这里指定为2!!
		btn.setOnClickListener{ 
			dbHelper.writableDatabase // 这里getWritableDatabase 
		}
	}
}

3.添加数据项

val dbHelper = MyDatabaseHelper(this, "BookStore.db",2)
val db = dbHelper.writableDatabase
val values = ContentValues().apply{
	put("name", "The Da Vinci Code")
	put("author", "Dan Brown")
	put("pages", 510)
	put("price", 16.96)
}
db.insert("Book", null, values)
// db.insert("数据库名字", null, 插入的数据项value)

4.更新数据

val dbHelper = MyDatabaseHelper(this, "BookStore.db",2)
val db = dbHelper.writableDatabase
val values = ContentValues()
values.put("price", 10.99)
db.update("Book",values, "name = ?",arrayOf("The Da Vinci Code"))
// db.insert("数据库名字", null, 插入的数据项value)

5.删除数据

delete(a,b,c)三个参数,a:表名 ,b、c用于约束删除某一行或几行

val db = dbHelper.writableDatabase
db.delete("Book", "pages > ?", arrayOf("500"))

6.查询数据

val db = dbHelper.writableDatabase
//select name ,price from Book where pages>500
val cursor = db.query("Book", arrayOf("name", "price"),"pages > ?", arrayOf("500"),null ,null, null)
//select * from Book
val cursor = db.query("Book", null, null, null, null ,null, null)

db.query()7个参数
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值