· 8 years ago · May 10, 2018, 09:30 AM
1package com.example.nidal.myfirstapp2;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8
9/**
10 * Created by nidal on 8/24/2017.
11 */
12
13public class roomDataBase extends SQLiteOpenHelper {
14
15 public static final String DATABASE_OF_ROOMS_NAME = "roomDataBase";
16 public static final String TABLE_OF_ROOMS_NAME = "Room_table";
17
18 public static final String ROOM_ID = "roomID";
19 public static final String ROOM_NAME = "name";
20 public static final String CAPACITY = "capacity";
21 public static final String HOST = "host";
22 public static final String ACTIVITY = "activity";
23 public static final String POPULATION = "population";
24
25
26
27 private static String EMPLOYEE_TABLE_CREATION = "CREATE TABLE IF NOT EXISTS " + TABLE_OF_ROOMS_NAME
28 + "(" +ROOM_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
29 + ROOM_NAME + " TEXT,"
30 + HOST + " TEXT,"
31 + POPULATION + " INTEGER,"
32 + ACTIVITY + " TEXT,"
33 + CAPACITY + " INTEGER)";
34
35
36 public roomDataBase(Context context) {
37 super(context, DATABASE_OF_ROOMS_NAME, null, 1);
38 }
39
40
41 @Override
42 public void onCreate(SQLiteDatabase sqLiteDatabase) {
43 sqLiteDatabase.execSQL(EMPLOYEE_TABLE_CREATION);
44 }
45
46 @Override
47 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
48 sqLiteDatabase.execSQL("DROP IF TABLE EXISTS " + TABLE_OF_ROOMS_NAME);
49 onCreate(sqLiteDatabase);
50 }
51
52
53 public boolean addRoom(Room room)
54 {
55 SQLiteDatabase db = this.getWritableDatabase();
56 ContentValues contentValues = new ContentValues();
57 contentValues.put(CAPACITY,room.getCapacity());
58 contentValues.put(HOST, room.getHostName());
59 contentValues.put(ACTIVITY , room.getActivity());
60 contentValues.put(ROOM_NAME , room.getRoomName());
61 contentValues.put(POPULATION, room.getCurrentResidents());
62 long res = db.insert(TABLE_OF_ROOMS_NAME,null,contentValues);
63 if(res == -1) {
64 return false;
65 }else
66 return true;
67 }
68
69 public Cursor getData()
70 {
71 SQLiteDatabase db = this.getWritableDatabase();
72 String query = "SELECT * FROM " + TABLE_OF_ROOMS_NAME;
73 Cursor data = db.rawQuery(query,null);
74 return data;
75 }
76
77 public Cursor getRoomID(String name){
78 SQLiteDatabase db = this.getWritableDatabase();
79 String query = "SELECT " + ROOM_ID + " FROM " + TABLE_OF_ROOMS_NAME +
80 " WHERE " + ROOM_NAME + " = " + "'" + name + "'";
81 Cursor data = db.rawQuery(query,null);
82 return data;
83 }
84
85 public void deleteName(int id , String name){
86 SQLiteDatabase db = this.getWritableDatabase();
87 String query = "DELETE FROM " + TABLE_OF_ROOMS_NAME + " WHERE "
88 + ROOM_ID + " = "+ "'" + id + "'" + " AND " + ROOM_NAME + " = "+ "'" + name + "'";
89 db.execSQL(query);
90 }
91
92
93 public void IncreaseCurrentPopulation(int roomID , int newAmount)
94 {
95 SQLiteDatabase db = this.getWritableDatabase();
96 String query = "UPDATE " + TABLE_OF_ROOMS_NAME + " SET " + POPULATION +
97 " = '" + newAmount + "'" +" WHERE " + ROOM_ID + " = '" + roomID + "'" ;
98 db.execSQL(query);
99 }
100
101}