Android Kotlin使用SQLite案例詳解
Kotlin使用SQLite
首先確定我們的目標(biāo),SQLite只是一種工具,我們需要掌握就是增刪改查就可以,我們真正需要?jiǎng)幽X的還是項(xiàng)目中的業(yè)務(wù)邏輯。我這篇文章寫(xiě)得比較適合新手,沒(méi)用過(guò)SQLite的同學(xué)。
前期準(zhǔn)備工作
新建一個(gè)類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?) { // 下面這個(gè)todo 如果不注釋掉的話就會(huì)報(bào)錯(cuò)。 // 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) } }
對(duì)數(shù)據(jù)進(jìn)行操作
操作比較簡(jiǎn)單,下面直接看代碼:
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) /** * 創(chuàng)建表 */ btnCreateDataBase.setOnClickListener { dbHelper.writableDatabase } /** * 添加數(shù)據(jù) */ btnAddData.setOnClickListener { val db=dbHelper.writableDatabase val Values1=ContentValues().apply { // 第一條數(shù)據(jù) 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 { // 第二條數(shù)據(jù) 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表中所有數(shù)據(jù) // 這里獲取到是Cursor對(duì)象 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>
到此這篇關(guān)于Android Kotlin使用SQLite案例詳解的文章就介紹到這了,更多相關(guān)Android Kotlin使用SQLite內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android使用criteria選擇合適的地理位置服務(wù)實(shí)現(xiàn)方法
這篇文章主要介紹了Android使用criteria選擇合適的地理位置服務(wù)實(shí)現(xiàn)方法,實(shí)例分析了Criteria的具體使用技巧,需要的朋友可以參考下2016-01-01android startActivityForResult的使用方法介紹
android startActivityForResult的使用方法介紹,需要的朋友可以參考一下2013-05-05Android短信驗(yàn)證碼倒計(jì)時(shí)驗(yàn)證的2種常用方式
各位開(kāi)發(fā)者們?cè)陂_(kāi)發(fā)中經(jīng)常會(huì)遇到獲取短信驗(yàn)證碼,獲取驗(yàn)證碼后需要等待1分鐘倒計(jì)時(shí),這段時(shí)間是不能再次發(fā)送短信請(qǐng)求的。這篇文章總結(jié)了兩種常用的Android​短信驗(yàn)證碼倒計(jì)時(shí)驗(yàn)證方式,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12Android實(shí)現(xiàn)登錄注冊(cè)功能封裝
Android應(yīng)用軟件基本上都會(huì)用到登錄注冊(cè)功能,本篇文章主要介紹了Android登錄注冊(cè)功能封裝功能實(shí)現(xiàn),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01Android實(shí)現(xiàn)分享長(zhǎng)圖并且添加全圖水印
這篇文章主要介紹了Android實(shí)現(xiàn)分享長(zhǎng)圖并且添加全圖水印的相關(guān)資料,需要的朋友可以參考下2017-03-03Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個(gè)數(shù)
本篇文章主要介紹了Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個(gè)數(shù),我們可以通過(guò)TextWatcher去觀察輸入框中輸入的內(nèi)容,有興趣的可以了解一下。2017-04-04Android 中自定義Dialog樣式的Activity點(diǎn)擊空白處隱藏軟鍵盤功能(dialog不消失)
項(xiàng)目中需要開(kāi)發(fā)帶有EditText的Dialog顯示,要求在編輯完EditText時(shí),點(diǎn)擊Dilog的空白處隱藏軟鍵盤。但是Dialog不會(huì)消失。下面通過(guò)實(shí)例代碼給大家分享實(shí)現(xiàn)方法,需要的的朋友參考下吧2017-04-04