package com.example.darshil

import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

class databasehelper (context: Context): SQLiteOpenHelper(context,db_name,null,db_version) {
    companion object{
        private val db_name = "dbbank"
        private val db_version=1
        private val tb_name="tblcustomer"
        private val column1="id"
        private val column2="name"
        private val column3="acc_no"
    }

    override fun onCreate(p0: SQLiteDatabase?) {
        val query="CREATE TABLE $tb_name($column1 INTEGER PRIMARY KEY,$column2 TEXT,$column3 TEXT)"
        p0?.execSQL(query)

    }

    override fun onUpgrade(
        p0: SQLiteDatabase?,
        p1: Int,
        p2: Int
    ) {
        val query="DROP TABLE $tb_name"
        p0?.execSQL(query)
        onCreate(p0)
    }

    fun addcustomer(customerlist:customer) : Boolean{
        var db = this.writableDatabase
        var values = ContentValues()
        values.put(column1,customerlist.id)
        values.put(column2,customerlist.name)
        values.put(column3,customerlist.acno)
        var result = db.insert(tb_name,null,values)

        if(Integer.parseInt(result.toString())==-1) {
            return false
        } else {
            return true
        }
    }

    fun deletecustomer(customerlist: customer) : Boolean{
        var db = this.writableDatabase
        var values = ContentValues()
        values.put(column1,customerlist.id)
        var result = db.delete(tb_name,"id=" + customerlist.id , null)
        if(Integer.parseInt(result.toString())==-1) {
            return false
        } else {
            return true
        }
    }

    fun updatecustomer(customerlist:customer):Boolean {
        var db = this.writableDatabase
        var values = ContentValues()
        values.put(column1, customerlist.id)
        values.put(column2, customerlist.name)
        values.put(column3, customerlist.acno)

        var result = db.update(tb_name,values,"id=" + customerlist.id , null)

        if(Integer.parseInt(result.toString())==-1) {
            return false
        } else {
            return true
        }
    }



//    fun viewcustomer():Cursor{
//        var db = this.readableDatabase
//        var cursor : Cursor? = null
//        cursor = db.rawQuery("SELECT * FROM $tb_name",null)
//        return cursor
//    }

    fun viewcustomer(): Cursor {
        val db = this.readableDatabase
        return db.rawQuery("SELECT * FROM tblcustomer", null)
    }

}


