Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

package com.example.

android_miniproject;

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

public class DBHelper extends SQLiteOpenHelper {


public DBHelper(Context context) {
super(context, "patient.db", null, 1);
}

public void onCreate(SQLiteDatabase db) {


db.execSQL("create table PatientDetails(Id Text primary key, Patient_Name
Text,Phone_number Text, Disease Text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("drop table if exists PatientDetails");
}

public boolean insertdata(String Id, String Patient_Name, String Phone_number,


String Disease) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("Id", Id);
cv.put("Patient_Name", Patient_Name);
cv.put("Phone_number", Phone_number);
cv.put("Disease", Disease);
long result = database.insert("PatientDetails", null, cv);
if (result == -1)
return false;
else
return true;
}

public boolean updatedata(String Id, String Patient_Name, String Phone_number,


String Disease) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("Phone_number", Phone_number);
cv.put("Disease", Disease);

Cursor cursor = database.rawQuery("Select * from PatientDetails where


Patient_Name = ?", new String[]{Patient_Name});
if (cursor.getCount() > 0) {
long result = database.update("PatientDetails", cv, "Patient_Name=?",
new String[]{Patient_Name});
if (result == -1)
return false;
else
return true;
}
else
return false;
}
public boolean deletedata(String Patient_Name) {
SQLiteDatabase database = this.getWritableDatabase();

Cursor cursor = database.rawQuery("Select * from PatientDetails where


Patient_Name = ?", new String[]{Patient_Name});
if (cursor.getCount() > 0) {
long result = database.delete("PatientDetails", "Patient_Name=?", new
String[]{Patient_Name});
if (result == -1)
return false;
else
return true;
}
else
return false;
}

public Cursor getdata() {


SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery("Select * from PatientDetails",null);
return cursor;

You might also like