Detailed explanation of Android Kotlin using SQLite case
- 2021-12-13 17:15:29
- OfStack
Kotlin uses SQLite
First of all, determine our goal. SQLite is only a tool. We need to master the addition, deletion and modification. What we really need to think about is the business logic in the project. I write this article is more suitable for novices and students who have never used SQLite.
Preparatory work
Create a new class MyDataBaseHelper that inherits from SQLiteOpenHelper with the following code:
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?) {
// This one below todo If you don't comment it out, you will report an error.
// 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)
}
}
Operating on data
The operation is relatively simple, look at the code directly below:
In 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)
/**
* Create a table
*/
btnCreateDataBase.setOnClickListener {
dbHelper.writableDatabase
}
/**
* Add data
*/
btnAddData.setOnClickListener {
val db=dbHelper.writableDatabase
val Values1=ContentValues().apply {
// No. 1 1 Bar data
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 {
// No. 1 2 Bar data
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
// Query Book All data in the table
// What you get here is Cursor Object
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()
}
}
}
Layout file
<?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>