· 5 years ago · Jul 08, 2020, 03:46 PM
1MyDBHandler.kt
2=================
3package com.example.mysqllite
4
5import android.content.ContentValues
6import android.content.Context
7import android.database.sqlite.SQLiteDatabase
8import android.database.sqlite.SQLiteOpenHelper
9
10class MyDBHandler(
11 context: Context,
12 name: String?,
13 factory: SQLiteDatabase.CursorFactory?,
14 version: Int
15) : SQLiteOpenHelper(context, DATABASE_NAME, factory, DATABASE_VERSION) {
16 override fun onCreate(db: SQLiteDatabase) {
17 val CREATE_PRODUCTS_TABLE =
18 ("CREATE TABLE IF NOT EXITS $TABLE_PRODUCTS ($COLUMN_ID INTEGER PRIMARY KEY, $COLUMN_PRODUCTNAME TEXT, $COLUMN_QUANTITY INTEGER)")
19 db.execSQL(CREATE_PRODUCTS_TABLE)
20 }
21
22 override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
23 /* it will erase yours existing table with all it's data without asking nothing
24 db.execSQL("DROP TABLE IF EXISTS ${TABLE_PRODUCTS}")
25 onCreate(db)
26 */
27 }
28
29 companion object {
30 private val DATABASE_VERSION = 1
31 private val DATABASE_NAME = "productDB.db"
32 val TABLE_PRODUCTS = "products"
33
34 //declare of columns names
35 val COLUMN_ID = "_id"
36 val COLUMN_PRODUCTNAME = "productname"
37 val COLUMN_QUANTITY = "quantity"
38 }
39
40 //add product
41 fun addProduct(product: Product) {
42 //give values to our database data
43 val values = ContentValues()
44 values.put(COLUMN_PRODUCTNAME, product.productName)
45 values.put(COLUMN_QUANTITY, product.quantity)
46
47 //give write access to the database
48 val db = this.writableDatabase
49
50 //insert the data into the database
51 db.insert(TABLE_PRODUCTS, null, values)
52
53 //close the database
54 db.close()
55 }
56
57 //find product
58 fun findProduct (productName:String):Product?{
59 //SELECT * FROM TABLE WHERE COLUMN = "" \" -> "
60 val query = "SELECT * FROM $TABLE_PRODUCTS WHERE $COLUMN_PRODUCTNAME = \"$productName\""
61 val db = this.writableDatabase
62 val cursor = db.rawQuery(query,null)
63 var product:Product? = null
64
65 // [product 1] [product 2] ... [product N]
66 if (cursor.moveToFirst()){
67 cursor.moveToFirst()
68
69 val id = Integer.parseInt(cursor.getString(0))
70 val name = cursor.getString(1)
71 val quantity = Integer.parseInt(cursor.getString(2))
72
73 product = Product(id,name,quantity)
74 cursor.close()
75 }
76 db.close()
77 return product
78 }
79
80 //delete product
81 fun deleteProduct(productName: String):Boolean{
82 var result = false
83 val query = "SELECT * FROM $TABLE_PRODUCTS WHERE $COLUMN_PRODUCTNAME = \"$productName\""
84
85 val db = this.writableDatabase
86 val cursor = db.rawQuery(query,null)
87
88 if (cursor.moveToFirst()){
89 val id = Integer.parseInt(cursor.getString(0))
90 db.delete(TABLE_PRODUCTS, "$COLUMN_ID = ?", arrayOf(id.toString()))
91 cursor.close()
92 result = true
93 }
94 db.close()
95 return result
96 }
97}
98
99
100Product.kt
101===================
102package com.example.mysqllite
103
104class Product {
105 //id
106 //productName
107 //quantity
108
109 //id,productName,quantity
110 //productName,quantity
111
112
113 var id: Int = 0
114 var productName: String? = null
115 var quantity: Int = 0
116
117 constructor(id: Int, productName: String, quantity: Int) {
118 this.id = id
119 this.productName = productName
120 this.quantity = quantity
121 }
122
123 constructor(productName: String, quantity: Int){
124 this.productName=productName
125 this.quantity=quantity
126 }
127
128}