@@ -16,14 +16,13 @@ public class DBHandler extends SQLiteOpenHelper {
1616 private static final String TABLE_NAME = "employee" ;
1717
1818
19-
2019 public DBHandler (Context context , String name , SQLiteDatabase .CursorFactory factory , int version ) {
2120 super (context , DB_NAME , factory , DB_VERSION );
2221 }
2322
2423 @ Override
25- public void onCreate (SQLiteDatabase db ){
26- String SQL_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "( ID INTEGER,"
24+ public void onCreate (SQLiteDatabase db ) {
25+ String SQL_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "( ID INTEGER,"
2726 + "NAME TEXT,"
2827 + "PASSWORD TEXT,"
2928 + "SSN TEXT,"
@@ -34,47 +33,42 @@ public void onCreate(SQLiteDatabase db){
3433 + "SALARY INTEGER,"
3534 + "BIRTHDAY DATE)" ;
3635 db .execSQL (SQL_CREATE_TABLE );
37- addHandler (db , new Employee (99999 ,"Admin" ,"seedadmin" , "43254314" ,
38- "" , "" , "" , "" , 400000 ,"1990-03-05" ));
39- addHandler (db , new Employee (10000 ,"Alice" ,"seedalice" , "10211002" ,
40- "" , "" , "" , "" , 20000 ,"2000-09-20" ));
41- addHandler (db , new Employee (20000 ,"Boby" ,"seedboby" , "10213352" ,
42- "" , "" , "" , "" , 50000 ,"2000-04-20" ));
43- addHandler (db , new Employee (30000 ,"Ryan" ,"seedryan" , "32193525" ,
44- "" , "" , "" , "" , 90000 ,"2000-04-10" ));
45- addHandler (db , new Employee (30000 , "Samy" ,"seedsamy" , "32111111" ,
46- "" , "" , "" , "" , 40000 ,"2000-01-11" ));
47- addHandler (db , new Employee (40000 , "Ted" ,"seedted" , "24343244" ,
48- "" , "" , "" , "" , 110000 ,"2000-11-3" ));
36+ addHandler (db , new Employee (99999 , "Admin" , "admin" , "43254314" ,
37+ "Ad" , "(403)220-1191" , "[email protected] " , "Gryffindor House" , 400000 ,
"1990-03-05" ));
38+ addHandler (db , new Employee (10000 , "Alice" , "alice" , "10211002" ,
39+ "Ali" , "(400)210-2112" , "[email protected] " , "Gryffindor House" , 20000 ,
"2000-09-20" ));
40+ addHandler (db , new Employee (20000 , "Boby" , "boby" , "10213352" ,
41+ "Bob" , "(404)789-2313" , "[email protected] " , "Hufflepuff House" , 50000 ,
"2000-04-20" ));
42+ addHandler (db , new Employee (30000 , "Ryan" , "ryan" , "32193525" ,
43+ "Ryanny" , "(210)096-3287" , "[email protected] " , "Ravenclaw House" , 90000 ,
"2000-04-10" ));
44+ addHandler (db , new Employee (40000 , "Samy" , "samy" , "32111111" ,
45+ "Sam" , "(450)218-8876" , "[email protected] " , "Slytherin" , 40000 ,
"2000-01-11" ));
46+ addHandler (db , new Employee (50000 , "Ted" , "ted" , "24343244" ,
47+ "Teddy" , "(208)222-8712" , "[email protected] " , "Azkaban" , 110000 ,
"2000-11-3" ));
4948 }
5049
5150 @ Override
52- public void onUpgrade (SQLiteDatabase db , int i , int i1 )
53- {
54- db .execSQL ("DROP TABLE IF EXISTS " + TABLE_NAME );
51+ public void onUpgrade (SQLiteDatabase db , int i , int i1 ) {
52+ db .execSQL ("DROP TABLE IF EXISTS " + TABLE_NAME );
5553 onCreate (db );
5654 }
5755
58- public Cursor loadHandler ()
59- {
56+ public Cursor loadHandler () {
6057 String query = "SELECT * FROM " + TABLE_NAME ;
6158 Cursor cursor = null ;
62- SQLiteDatabase db = this .getWritableDatabase ();
63- if (db != null )
64- {
65- cursor = db .rawQuery (query ,null );
66- db .close ();
59+ SQLiteDatabase db = this .getReadableDatabase ();
60+ if (db != null ) {
61+ cursor = db .rawQuery (query , null );
62+ // db.close();
6763 }
6864
6965 return cursor ;
7066 }
7167
72- public void addHandler (SQLiteDatabase db , Employee employee )
73- {
68+ public void addHandler (SQLiteDatabase db , Employee employee ) {
7469 DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd" );
7570 ContentValues values = new ContentValues ();
76- if (db !=null )
77- {
71+ if (db != null ) {
7872 values .put ("ID" , employee .getId ());
7973 values .put ("NAME" , employee .getName ());
8074 values .put ("PASSWORD" , employee .getPassword ());
@@ -89,13 +83,12 @@ public void addHandler(SQLiteDatabase db, Employee employee)
8983 }
9084 }
9185
92- public Employee findHandler (String username , String password )
93- {
94- String query = "SELECT * FROM " + TABLE_NAME + " WHERE NAME='" + username + "' AND PASSWORD='" + password +"'" ;
95- SQLiteDatabase db = this .getWritableDatabase ();
96- Employee employee = null ;
97- Cursor cursor = db .rawQuery (query ,null );
98- if (cursor !=null && cursor .getCount ()>0 && cursor .moveToFirst ()) {
86+ public Employee findHandler (String username , String password ) {
87+ String query = "SELECT * FROM " + TABLE_NAME + " WHERE NAME='" + username + "' AND PASSWORD='" + password + "'" ;
88+ SQLiteDatabase db = this .getReadableDatabase ();
89+ Employee employee = null ;
90+ Cursor cursor = db .rawQuery (query , null );
91+ if (cursor != null && cursor .getCount () > 0 && cursor .moveToFirst ()) {
9992 employee = new Employee (Integer .parseInt (cursor .getString (0 )),
10093 cursor .getString (1 ),
10194 cursor .getString (2 ),
@@ -106,16 +99,16 @@ public Employee findHandler(String username, String password)
10699 cursor .getString (7 ),
107100 Integer .parseInt (cursor .getString (8 )),
108101 cursor .getString (9 )
109- );
102+ );
110103 cursor .close ();
111104 }
112105 db .close ();
113106 return employee ;
114107 }
115108
116- public boolean updateHandler (Employee employee )
117- {
118- String UPDATE_SQL_COMMAND = String .format ("UPDATE %s SET NICKNAME=%s , EMAIL=%s , ADDRESS=%s , PASSWORD=%s , PHONE=%s WHERE ID=%s RETURNING * " ,
109+ public void partialUpdateHandler (Employee employee ) {
110+ // invoked by user, update some optional fields
111+ String UPDATE_SQL_COMMAND = String .format ("UPDATE %s SET NICKNAME='%s' , EMAIL='%s' , ADDRESS='%s' , PASSWORD='%s' , PHONE='%s' WHERE ID=%s" ,
119112 TABLE_NAME ,
120113 employee .getNickname (),
121114 employee .getEmail (),
@@ -124,9 +117,25 @@ public boolean updateHandler(Employee employee)
124117 employee .getPhone (),
125118 employee .getId ());
126119 SQLiteDatabase db = this .getWritableDatabase ();
127- Cursor cursor = db .rawQuery (UPDATE_SQL_COMMAND ,null );
128- cursor .close ();
129- return cursor .getCount ()==1 ;
120+ db .execSQL (UPDATE_SQL_COMMAND );
121+ }
122+
123+ public boolean fullUpdateHandler (Employee employee )
124+ {
125+ // invoked by admin, update all fields except ID
126+ DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd" );
127+ SQLiteDatabase db = this .getWritableDatabase ();
128+ ContentValues values = new ContentValues ();
129+ values .put ("NAME" , employee .getName ());
130+ values .put ("PASSWORD" , employee .getPassword ());
131+ values .put ("SSN" , employee .getSsn ());
132+ values .put ("NICKNAME" , employee .getNickname ());
133+ values .put ("PHONE" , employee .getPhone ());
134+ values .put ("SALARY" , employee .getSalary ());
135+ values .put ("ADDRESS" , employee .getAddress ());
136+ values .put ("EMAIL" , employee .getEmail ());
137+ values .put ("BIRTHDAY" , dateFormat .format (employee .getBirthday ()));
138+ return -1 !=db .update (TABLE_NAME ,values ,"ID=?" , new String []{String .valueOf (employee .getId ())});
130139 }
131140
132141}
0 commit comments