0% found this document useful (0 votes)
18 views3 pages

Pertemuan 14 - Mobile Programming (SQLite - Bagian2) - Yozika

The document discusses SQLite database operations in Android, including reading, updating, and deleting data. It provides code to: 1. Add methods to a DataBaseHandler class to read, update, and delete data from a database table 2. Update an activity's layout and code to call these methods and display the results 3. On running the program, buttons allow reading existing data, updating the data by incrementing ages, and deleting all data. The read operation displays the retrieved data in a text view.

Uploaded by

Ridho F
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Pertemuan 14 - Mobile Programming (SQLite - Bagian2) - Yozika

The document discusses SQLite database operations in Android, including reading, updating, and deleting data. It provides code to: 1. Add methods to a DataBaseHandler class to read, update, and delete data from a database table 2. Update an activity's layout and code to call these methods and display the results 3. On running the program, buttons allow reading existing data, updating the data by incrementing ages, and deleting all data. The read operation displays the retrieved data in a text view.

Uploaded by

Ridho F
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PERTEMUAN 14 Mobile Programming

SQLite (Bagian 2)

1. Buka file kotlin DataBaseHandler.kt dan tambahkan

//Read Data
@SuppressLint("Range")
fun readData() : MutableList<User1> {
var list = ArrayList<com.itpln.latihansqlite.User>()
val db = this.readableDatabase
val query = "Select * from " + TABLE_NAME
val result = db.rawQuery(query, null)
if (result.moveToFirst() ){
do {
var user = User1()
user.id =
result.getString(result.getColumnIndex(COL_ID)).toInt()
user.name =
result.getString(result.getColumnIndex(COL_NAME))
user.age =
result.getString(result.getColumnIndex(COL_AGE)).toInt()
list.add(user)

}while (result.moveToNext())
}
result.close()
db.close()
return list

}
//Delete Data
fun deleteData() {
val db = this.writableDatabase
db.delete(TABLE_NAME, null, null)
db.close()

//Update Data
fun updateData() {
val db = this.writableDatabase
val query = "Select * from " + TABLE_NAME
val result = db.rawQuery(query, null)
if (result.moveToFirst() ){
do {
var cv = ContentValues()
cv.put(COL_AGE,result.getInt(2)+1)
db.update(TABLE_NAME,cv, COL_ID + "=? AND " + COL_NAME
+ "=?",
arrayOf(result.getString(0),
result.getString(1)))
}while (result.moveToNext())
}
result.close()
db.close()
}
}

2. Tambahkan kode program di file activity_main.xml

Perhatikan/samakan dengan kodingan bagian sebelumnya untuk MainActivity.kt

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val btn_insert = findViewById<Button>(R.id.btn_insert)


val btn_update = findViewById<Button>(R.id.btn_update)
val btn_delete = findViewById<Button>(R.id.btn_delete)
val btn_read = findViewById<Button>(R.id.btn_read)
val tvResult = findViewById<TextView>(R.id.tvResult)
val etvName = findViewById<EditText>(R.id.etvname)
val etvAge = findViewById<EditText>(R.id.etvage)
val context = this
var db = DataBaseHandler(context)

btn_insert.setOnClickListener ({
if(etvName.text.toString().length > 0 &&
etvAge.text.toString().length > 0){
var user = User(etvName.text.toString(),
etvAge.text.toString().toInt())

db.insertData(user)
} else {
Toast.makeText(context,"Please Fill All Data's",
Toast.LENGTH_SHORT).show()
}
})

btn_read.setOnClickListener ({
var data = db.readData()
tvResult.text =""
for (i in 0.. (data.size-1)) {
tvResult.append(data.get(i).id.toString() + " " +
data.get(i).name + " " + data.get(i).age + "\n")
}

})

btn_update.setOnClickListener ({
db.updateData()
btn_read.performClick()
})
btn_delete.setOnClickListener ({
db.deleteData()
btn_read.performClick()
})
}
}

8. Jalankan Program : jika sukses maka akan muncul seperti dibawah :

a. Ketika tombol read di klik akan muncul hasil data yang sudah di inputkan

b. jika di klik update data akan ter update

c. jika di klik delete, data akan terhapus

You might also like