Unit 5
Unit 5
-(ACTION_IMAGE_CAPTURE Intent
of MediaStore class.)
❖ What is SQLite?
➢ SQLite is a C-Language library which implements a SQL
database engine.
➢SQLite is a database engine written in the C
programming language.
➢ It boasts that it is the most used database engine in the
world-as it is bundled in mobile and desktop software of all
kinds. Some of the key features of SQLite include:
● Stability: SQLite is resilient in the face of corrupt inputs,
including maliciously designed database files and SQL
strings.
● Cross-Platform Compatible: Databases can be copied
between 64 and 32 bit systems.
● Backwards Compatible: Backwards compatibility
constraints mean that SQLite is only able to store
values that are NULL, integers, floating-point numbers,
text, and BLOBs.
● Small Size: The entire library is less then 600Kb.
● Precompiled Binaries
You can download this file from the SQLite website given
below - https://www.sqlite.org/download.html.
The software will then open in the command line and you can
execute any sql commands:
● Here, you successfully installed Sqlite.
● Now,Download DB browser for sqlite from here
https://sqlitebrowser.org/dl/
➢ SQLiteOpenHelper class
The android.database.sqlite.SQLiteOpenHelper class is used
for database creation and version management. For
performing any database operation, you have to provide the
implementation of onCreate() and onUpgrade() methods of
SQLiteOpenHelper class.
Constructor Description
handler.
Method Description
object.
newVersion)
➢ SQLiteDatabase class
It contains methods to be performed on the SQLite database
such as create, update, delete, select etc.
Method Description
execSQL(String sql): Executes the SQL query, not a select
Unit query.
to be stored.
ContentValues
values, String
whereClause, String[]
whereArgs): Int
query(String table, Returns a cursor over the resultset.
String[] columns,
String selection,
String[]
selectionArgs, String
groupBy, String
having, String
orderBy): Cursor
AndroidManifest.xml
<uses-permission
android:name="android.permission.READ_EXTERNAL_STOR
AGE" />
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/a
ndroid"
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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
Mainactivity.kt
package com.example.crudsqlite
import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import
androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import
com.example.crudsqlite.ui.theme.CrudsqliteTheme
val addName:Button =
findViewById(R.id.addName)
val printName:Button =
findViewById(R.id.printName)
val enterName:EditText =
findViewById(R.id.enterName)
val enterAge:EditText =
findViewById(R.id.enterAge)
// below code is to add on click
// listener to our add name button
addName.setOnClickListener{
Name.append(cursor.getString(cursor.getColumnIndex(
DBhelper.NAME_COl)) + "\n")
Age.append(cursor.getString(cursor.getColumnIndex(D
Bhelper.AGE_COL)) + "\n")
Name.append(cursor.getString(cursor.getColumnIndex(
DBhelper.NAME_COl)) + "\n")
Age.append(cursor.getString(cursor.getColumnIndex(D
Bhelper.AGE_COL)) + "\n")
}
}
}
MyDBhelper.kt
package com.example.crudsqlite
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
// at last we are
// closing our database
db.close()
}
companion object {
// here we have defined variables for our
database
DBhelper.kt
package com.example.crudsqlite
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
values.put(NAME_COl, name)
values.put(AGE_COL, age)
val db = this.writableDatabase
db.insert(TABLE_NAME, null, values)
}
//View all records
fun getName(): Cursor? {
val db = this.readableDatabase
return db.rawQuery("SELECT * FROM " + TABLE_NAME,
null)
}
//Update
fun update_tbl(name: String, age: String) {
val db = this.writableDatabase
val values = ContentValues()
values.put(NAME_COl, name)
values.put(AGE_COL, age)
db.update(TABLE_NAME, values, "name=?",
arrayOf(name))
db.close() }
//Delete
fun delete_data(name:String,age: String) {
val db = this.writableDatabase
db.delete(TABLE_NAME,"name=?", arrayOf(name))
db.close()
}
//create object
companion object {
private val DATABASE_NAME = "MYDB"
private val DATABASE_VERSION = 1
val TABLE_NAME = "STUD"
val ID_COL = "id"
val NAME_COl = "name"
val AGE_COL = "age"
}
}
MainActivity.kt
package com.example.crudsqlite
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
printName.setOnClickListener{
val db = DBhelper(this, null)
val cursor = db.getName()
cursor!!.moveToFirst()
val Name:TextView = findViewById(R.id.Name)
val Age:TextView = findViewById(R.id.Age)
Name.append(cursor.getString(cursor.getColumnIndex
(DBhelper.NAME_COl)) + "\n")
Age.append(cursor.getString(cursor.getColumnIndex
(DBhelper.AGE_COL)) + "\n")
while(cursor.moveToNext()){
Name.append(cursor.getString(cursor.getColumnIndex
(DBhelper.NAME_COl)) + "\n")
Age.append(cursor.getString(cursor.getColumnIndex
(DBhelper.AGE _COL)) + "\n") }}
updateName.setOnClickListener {
val myDb = DBhelper(this, null)
myDb.update_tbl(name =
enterName.text.toString(),age = enterAge.text.toString())
enterName.text.clear()
enterAge.text.clear()}
deleteName.setOnClickListener {
val mydb = DBhelper(this,null)
mydb.delete_data(name =
enterName.text.toString(), age = enterAge.text.toString())
enterName.text.clear()
enterAge.text.clear() }
}
}
layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update" />
<Button
android:id="@+id/btndelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
Output:-