· 8 years ago · Jun 11, 2018, 05:08 AM
1package com.example.bharat.generalknowledge.dbhandler
2
3import android.content.ContentValues
4import android.content.Context
5import android.database.sqlite.SQLiteDatabase
6import android.database.sqlite.SQLiteOpenHelper
7import android.util.Log
8
9class BlueprintHandler(context: Context) : SQLiteOpenHelper(context, Database.DATABASE_NAME, null, Database.DATABASE_VERSION) {
10 companion object {
11 private const val SQL_CREATE_ENTRIES = "CREATE TABLE IF NOT EXISTS `${Blueprint.BlueprintEntry.TABLE_NAME}` (\n" +
12 " `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
13 " `cat_id` INTEGER NOT NULL UNIQUE,\n" +
14 " `mark` INTEGER NOT NULL,\n" +
15 ")"
16 private const val SQL_CREATE_INDEXING = "CREATE INDEX IF NOT EXISTS cat_id ON `${Blueprint.BlueprintEntry.TABLE_NAME}` (${Blueprint.BlueprintEntry.COLUMN_CAT_ID});"
17 private const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${Blueprint.BlueprintEntry.TABLE_NAME}"
18 }
19
20 override fun onCreate(db: SQLiteDatabase) {
21 try {
22 db.execSQL(SQL_CREATE_ENTRIES)
23 db.execSQL(SQL_CREATE_INDEXING)
24 } catch (e: Exception) {
25 Log.e("CREATE BLUEPRINT TABLE", e.toString())
26 }
27
28
29 }
30
31 override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
32 // This database is only a cache for online data, so its upgrade policy is
33 // to simply to discard the data and start over
34 try {
35 db.execSQL(SQL_DELETE_ENTRIES)
36 onCreate(db);
37 } catch (e: Exception) {
38 Log.e("DELETE BLUEPRINT TABLE", e.toString())
39 }
40 }
41
42 override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
43 onUpgrade(db, oldVersion, newVersion)
44 }
45
46 // code to add the new contact
47 fun addBlueprint(blueprint: Blueprint) {
48 try {
49 val db = this.writableDatabase
50
51 val values = ContentValues().apply {
52 // put(Blueprint.BlueprintEntry.COLUMN_ID, blueprint.id)
53 put(Blueprint.BlueprintEntry.COLUMN_CAT_ID, blueprint.cat_id)
54 put(Blueprint.BlueprintEntry.COLUMN_MARK, blueprint.mark)
55 }
56 // Insert the new row, returning the primary key value of the new row
57 val newRowId = db?.insert(Blueprint.BlueprintEntry.TABLE_NAME, null, values)
58 } catch (e: Exception) {
59 Log.e("INSERT BLUEPRINT ROW", e.toString())
60 }
61 }
62
63 fun updateBlueprint(id: Int, blueprint: Blueprint): Boolean {
64 val db = this.writableDatabase
65 val values = ContentValues().apply {
66// put(Blueprint.BlueprintEntry.COLUMN_ID, blueprint.id)
67 put(Blueprint.BlueprintEntry.COLUMN_CAT_ID, blueprint.cat_id)
68 put(Blueprint.BlueprintEntry.COLUMN_MARK, blueprint.mark)
69 }
70
71 val selection = "${Blueprint.BlueprintEntry.COLUMN_ID} = ?"
72 val selectionArgs = arrayOf(id.toString())
73 db.update(
74 Blueprint.BlueprintEntry.TABLE_NAME,
75 values,
76 selection,
77 selectionArgs
78 )
79
80 return true
81 }
82
83 // Get records with this Blueprint
84 fun getBlueprintWithCatId(cat_id: Int): Blueprint{
85 val db = this.readableDatabase
86 val cursor = db.rawQuery("SELECT *FROM ${Blueprint.BlueprintEntry.TABLE_NAME} WHERE `${Blueprint.BlueprintEntry.COLUMN_CAT_ID}` = ${cat_id}", null)
87
88 cursor.moveToFirst();
89 val catObj = Blueprint(
90// cursor.getInt(cursor.getColumnIndex(Blueprint.BlueprintEntry.COLUMN_ID)),
91 cursor.getInt(cursor.getColumnIndex(Blueprint.BlueprintEntry.COLUMN_CAT_ID)),
92 cursor.getInt(cursor.getColumnIndex(Blueprint.BlueprintEntry.COLUMN_MARK))
93 )
94 return catObj
95 }
96}