
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/id" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="number" android:hint="id" /> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="Name" /> <EditText android:id="@+id/mobile" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="phone" android:hint="Mobile"/> <EditText android:id="@+id/email" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textEmailAddress" android:hint="Email"/> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" android:hint="Password"/> <Button android:id="@+id/save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Save" /> <Button android:id="@+id/getall" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="getall" /> <Button android:id="@+id/update" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Update" /> <Button android:id="@+id/delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Delete" /> </LinearLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package com.androindian.sqlite import android.os.Bundle import android.view.View import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { var dataBaseHelper: DataBaseHelper? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView( R.layout.activity_main) dataBaseHelper = DataBaseHelper(this@MainActivity) //save save.setOnClickListener { val insert = dataBaseHelper!!.insertdData( name.text.toString().trim { it <= ' ' }, email.text.toString().trim { it <= ' ' }, mobile.text.toString().trim { it <= ' ' }, password.text.toString().trim { it <= ' ' }) if (insert == true) { Toast.makeText(this@MainActivity, "Sucess", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this@MainActivity, "Failed", Toast.LENGTH_SHORT).show() } } //get getall.setOnClickListener(View.OnClickListener { val res = dataBaseHelper!!.alldata if (res.count == 0) { Toast.makeText(this@MainActivity, "Nodata", Toast.LENGTH_SHORT).show() return@OnClickListener } val buffer = StringBuffer() while (res.moveToNext()) { buffer.append("Id " + res.getString(0) + "\n") buffer.append("name " + res.getString(1) + "\n") buffer.append("email " + res.getString(2) + "\n") buffer.append("mobile " + res.getString(3) + "\n") buffer.append("pass " + res.getString(4) + "\n") } ShowMessage("Data", buffer.toString()) }) //update update.setOnClickListener { val update = dataBaseHelper!!.updateData( id.text.toString().trim { it <= ' ' }, name.text.toString().trim { it <= ' ' }, email.text.toString().trim { it <= ' ' }, mobile.text.toString().trim { it <= ' ' }, password.text.toString().trim { it <= ' ' }) if (update == true) { Toast.makeText(this@MainActivity, "Sucess", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this@MainActivity, "Failed", Toast.LENGTH_SHORT).show() } } //delete delete.setOnClickListener { val delete = dataBaseHelper!!.deleteData(id.text.toString()) if (delete > 0) { Toast.makeText(this@MainActivity, "Sucess", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this@MainActivity, "Failed", Toast.LENGTH_SHORT).show() } } } private fun ShowMessage(data: String, toString: String) { val alert = AlertDialog.Builder(this@MainActivity) alert.setTitle(data) alert.setMessage(toString) alert.show() } } |
DataBaseHelper.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.androindian.sqlite 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, Databasename, null, 1) { override fun onCreate(db: SQLiteDatabase) { /*db.execSQL("create table "+ Tablename+ " "+ "(ID INTERGER PRIMARY KEY AUTOINCREMENT," + "NAME TEXT,EMAIL TEXT ,MOBILE TEXT,PASSWORD TEXT)");*/ db.execSQL("create table " + Tablename + " " + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + "NAME TEXT,EMAIL TEXT ,MOBILE TEXT,PASSWORD TEXT)") } override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { db.execSQL("DROP TABLE IF EXISTS $Tablename") onCreate(db) } fun insertdData(name: String?, email: String?, mobile: String?, password: String?): Boolean { val sqLiteDatabase = this.writableDatabase val contentValues = ContentValues() contentValues.put(Col2, name) contentValues.put(Col3, email) contentValues.put(Col4, mobile) contentValues.put(Col5, password) val res = sqLiteDatabase.insert(Tablename, null, contentValues) return if (res == -1L) { false } else { true } } val alldata: Cursor get() { val sqLiteDatabase = this.readableDatabase return sqLiteDatabase.rawQuery("select * from $Tablename", null) } fun updateData(id: String, name: String?, email: String?, mobile: String?, password: String?): Boolean { val sqLiteDatabase = this.writableDatabase val contentValues = ContentValues() contentValues.put(Col1, id) contentValues.put(Col2, name) contentValues.put(Col3, email) contentValues.put(Col4, mobile) contentValues.put(Col5, password) sqLiteDatabase.update(Tablename, contentValues, "ID= ?", arrayOf(id)) return true } fun deleteData(id: String): Int { val sqLiteDatabase = this.writableDatabase return sqLiteDatabase.delete(Tablename, "ID =?", arrayOf(id)) } companion object { const val Databasename = "Student.db" const val Tablename = "educations" const val Col1 = "ID" const val Col2 = "NAME" const val Col3 = "EMAIL" const val Col4 = "MOBILE" const val Col5 = "PASSWORD" } } |