zed_com

Tg

May 12th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. public class DataBaseHelper extends SQLiteOpenHelper {
  2.     public static final  String DATABASE_NAME = "Student.db";
  3.     public static final  String TABLE_NAME = "Student_table";
  4.  
  5.     public static final  String COL_1 = "ID";
  6.     public static final  String COL_2 = "NAME";
  7.     public static final  String COL_3 = "SURNAME";
  8.     public static final  String COL_4 = "MARKS";
  9.  
  10.     public DataBaseHelper(Context context) {
  11.         super(context, DATABASE_NAME, null, 1);
  12.     }
  13.  
  14.     @Override
  15.     public void onCreate(SQLiteDatabase db) {
  16.         db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
  17.     }
  18.  
  19.     @Override
  20.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  21.         db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
  22.     }
  23.  
  24.     public boolean insertData(String name,String surname,String marks){
  25.         SQLiteDatabase db = this.getWritableDatabase();
  26.         ContentValues contentValues = new ContentValues();
  27.         contentValues.put(COL_2,name);
  28.         contentValues.put(COL_3,surname);
  29.         contentValues.put(COL_4,marks);
  30.         long result = db.insert(TABLE_NAME,null,contentValues);
  31.         db.close();
  32.  
  33.         //To Check Whether Data is Inserted in DataBase
  34.         if(result==-1){
  35.             return false;
  36.         }else{
  37.             return true;
  38.         }
  39.     }
  40.  
  41. public boolean updateData(String id,String name,String surname,String marks){
  42.         SQLiteDatabase db = this.getWritableDatabase();
  43.         ContentValues contentValues = new ContentValues();
  44.         contentValues.put(COL_2,name);
  45.         contentValues.put(COL_3,surname);
  46.         contentValues.put(COL_4,marks);
  47.         int result =db.update(TABLE_NAME,contentValues,"ID =?",new String[]{id});
  48.         if(result>0){
  49.             return true;
  50.         }else
  51.         {
  52.             return false;
  53.         }
  54.     }
  55.  
  56. public Cursor getAllData(){
  57.         SQLiteDatabase db = this.getWritableDatabase();
  58.         Cursor res = db.rawQuery("Select * from " + TABLE_NAME,null);
  59.         return  res;
  60.     }
  61.  
  62. public Integer deleteData(String id){
  63.         SQLiteDatabase db = this.getWritableDatabase();
  64.         int i =db.delete(TABLE_NAME,"ID=?",new String[]{id});
  65.         return i;
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment