Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. package ru.rache.mobileappmanager.DBHelper
  2.  
  3. import android.content.ContentValues
  4. import android.content.Context
  5. import android.database.sqlite.SQLiteDatabase
  6. import android.database.sqlite.SQLiteOpenHelper
  7. import android.util.Log
  8. import ru.rache.mobileappmanager.db.Task
  9. import ru.rache.mobileappmanager.ui.home.FragmentTaskRemaining
  10.  
  11. class DbHelper(context: Context) : SQLiteOpenHelper(context, "tasks.db", null, 4) {
  12. companion object {
  13. public val TABLE_NAME: String = "Person"
  14. public val COL_ID: String = "_id"
  15. public val COL_NAME: String = "Name"
  16. public val COL_EMAIL: String = "Email"
  17. }
  18.  
  19. override fun onCreate(db: SQLiteDatabase?) {
  20. val CREATE_TABLE_QUERY: String = ("CREATE TABLE $TABLE_NAME($COL_ID INTEGER PRIMARYKEY, $COL_NAME TEXT, $COL_NAME TEXT) ")
  21. db!!.execSQL(CREATE_TABLE_QUERY);
  22. }
  23.  
  24. override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
  25. db!!.execSQL("DROP TABLE IF EXISTS $TABLE_NAME")
  26. onCreate(db!!)
  27. }
  28.  
  29. val allPerson:List<Task>
  30. get(){
  31. val lstPersons = ArrayList<Task>()
  32. val selectQuery = "SELECT * FROM $TABLE_NAME"
  33. val db = this.writableDatabase
  34. val cursor = db.rawQuery(selectQuery, null)
  35. if(cursor.moveToFirst()){
  36. do{
  37. val person = Task()
  38. person.id = cursor.getInt(cursor.getColumnIndex(COL_ID))
  39. person.name = cursor.getString(cursor.getColumnIndex(COL_NAME))
  40. person.email = cursor.getString(cursor.getColumnIndex(COL_EMAIL))
  41.  
  42. lstPersons.add(person)
  43. } while(cursor.moveToNext())
  44. }
  45. db.close()
  46. return lstPersons
  47. }
  48.  
  49. fun addPerson(person:Task){
  50. val db = this.writableDatabase
  51. val values = ContentValues()
  52. values.put(COL_ID, person.id)
  53. values.put(COL_NAME, person.name)
  54. values.put(COL_EMAIL, person.email)
  55.  
  56. db.insert(TABLE_NAME, null, values)
  57. db.close()
  58. }
  59.  
  60. fun updatePerson(person:Task):Int{
  61. val db = this.writableDatabase
  62. val values = ContentValues()
  63. values.put(COL_ID, person.id)
  64. values.put(COL_NAME, person.name)
  65. values.put(COL_EMAIL, person.email)
  66.  
  67. return db.update(TABLE_NAME, values, "$COL_ID=?", arrayOf(person.id.toString()))
  68. }
  69.  
  70. fun deletePerson(person:Task){
  71. val db = this.writableDatabase
  72.  
  73. db.delete(TABLE_NAME, "$COL_ID=?", arrayOf(person.id.toString()))
  74. db.close()
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement