package com.example.dbsqlite
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
@SuppressLint("Range")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val entN = findViewById<EditText>(R.id.enterName)
val entA = findViewById<EditText>(R.id.enterAge)
val btnAdd = findViewById<Button>(R.id.addData)
val btnShow = findViewById<Button>(R.id.showData)
val btnDrop = findViewById<Button>(R.id.dropData)
val btnUpd = findViewById<Button>(R.id.updData)
val shN = findViewById<TextView>(R.id.showName)
val shA = findViewById<TextView>(R.id.showAge)
btnAdd.setOnClickListener {
val db = DBHelper(this)
if (entN.text.trim().isEmpty() || entA.text.trim().isEmpty()){
Toast.makeText(this, "Ma'lumot kiritilmadi",Toast.LENGTH_SHORT).show()
}
else{
db.addData(entN.text.trim().toString(), entA.text.trim().toString().toInt())
Toast.makeText(this, "Ma'lumot bazaga qo'shildi",Toast.LENGTH_SHORT).show()
entN.text.clear()
entA.text.clear()
}
}
btnShow.setOnClickListener {
val db = DBHelper(this)
val cursor = db.showData()
shN.text=""
shA.text=""
cursor.moveToFirst()
shN.append(cursor.getString(cursor.getColumnIndex(DBHelper.COL_NAME))+"\n")
shA.append(cursor.getString(cursor.getColumnIndex(DBHelper.COL_AGE))+"\n")
while (cursor.moveToNext()){
shN.append(cursor.getString(cursor.getColumnIndex(DBHelper.COL_NAME))+"\n")
shA.append(cursor.getString(cursor.getColumnIndex(DBHelper.COL_AGE))+"\n")
}
}
btnDrop.setOnClickListener {
val db = DBHelper(this)
if (!entN.text.isEmpty()){
val result = db.dropData(entN.text.toString())
if (result == -1){
Toast.makeText(this, "Ma'lumotlar mavjud emas",Toast.LENGTH_SHORT).show()
}
else{
if(!shN.text.equals(entN.text))
Toast.makeText(this, "Bunday yozuv yo'q",Toast.LENGTH_SHORT).show()
else
Toast.makeText(this, "Ma'lumot o'chirildi",Toast.LENGTH_SHORT).show()
}
}
}
btnUpd.setOnClickListener {
val db = DBHelper(this)
if (entN.text.trim().isEmpty() || entA.text.trim().isEmpty()){
Toast.makeText(this, "Ma'lumot kiritilmadi",Toast.LENGTH_SHORT).show()
}
else{
db.updateData(entN.text.trim().toString(), entA.text.trim().toString().toInt())
Toast.makeText(this, "Ma'lumot yangilandi",Toast.LENGTH_SHORT).show()
entN.text.clear()
entA.text.clear()
}
}
}
}
package com.example.dbsqlite
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION){
override fun onCreate(p0: SQLiteDatabase) {
val query = "CREATE TABLE "+TABLE_NAME+"("+
COL_ID+" INTEGER PRIMARY KEY," +
COL_NAME+" TEXT," +
COL_AGE+" INTEGER)"
p0.execSQL(query)
}
override fun onUpgrade(p0: SQLiteDatabase, p1: Int, p2: Int) {
p0.execSQL("DROP TABLE IF EXISTS data")
onCreate(p0)
}
fun addData(name:String, age:Int){
val values = ContentValues()
values.put(COL_NAME, name)
values.put(COL_AGE, age)
val db = this.writableDatabase
db.insert(TABLE_NAME,null,values)
db.close()
}
fun showData() : Cursor{
val db = this.readableDatabase
return db.rawQuery("SELECT * FROM " + TABLE_NAME, null)
}
fun dropData(name: String): Int {
val db = this.writableDatabase
return db.delete(TABLE_NAME, COL_NAME + "=?", arrayOf(name))
}
fun updateData(name: String, age: Int){
val values = ContentValues()
values.put(COL_NAME, name)
values.put(COL_AGE, age)
val db = this.writableDatabase
db.update(TABLE_NAME,values, COL_NAME + "=?", arrayOf(name))
db.close()
}
companion object{
var DB_NAME = "myDB"
var DB_VERSION = 1
var TABLE_NAME = "data"
var COL_ID = "id"
var COL_NAME = "name"
var COL_AGE = "age"
}
}