]> iEval git - unical.git/blame - src/ro/ieval/unical/DatabaseInteract.java
Fix the three get functions
[unical.git] / src / ro / ieval / unical / DatabaseInteract.java
CommitLineData
bdd8f09c
PT
1package ro.ieval.unical;
2
7be52756
PT
3import java.util.ArrayList;
4import java.util.List;
5
819197b2 6import android.content.ContentValues;
bdd8f09c 7import android.content.Context;
7be52756 8import android.database.Cursor;
bdd8f09c
PT
9import android.database.SQLException;
10import android.database.sqlite.SQLiteDatabase;
11
12public class DatabaseInteract {
13 private SQLiteDatabase database;
14 private MySQLiteHelper dbHelper;
819197b2
PT
15 private String[] allColumns = {
16 MySQLiteHelper.ID, MySQLiteHelper.title,
17 MySQLiteHelper.description, MySQLiteHelper.date,
18 MySQLiteHelper.duration, MySQLiteHelper.repeat,
19 MySQLiteHelper.repeatIn, MySQLiteHelper.location
20 };
bdd8f09c
PT
21
22 public DatabaseInteract(Context context) {
23 dbHelper = new MySQLiteHelper(context);
7be52756 24 database=dbHelper.getWritableDatabase();
bdd8f09c
PT
25 }
26
27 public void open() throws SQLException {
28 database=dbHelper.getWritableDatabase();
29 }
30
31 public void close() {
32 dbHelper.close();
33 }
34
819197b2
PT
35 //Insert event
36 public void insertEvent(Event e) {
37 ContentValues values = new ContentValues();
7be52756
PT
38 values.put(allColumns[0], e._id);
39 values.put(allColumns[1], e.title);
40 values.put(allColumns[2], e.description);
41 values.put(allColumns[3], e.date);
42 values.put(allColumns[4], e.duration);
819197b2
PT
43 values.put(allColumns[5], e.repeat);
44 values.put(allColumns[6], e.repeatInterval);
45 values.put(allColumns[7], e.location);
46 database.insert(MySQLiteHelper.Name, null, values);
47 }
bdd8f09c 48 //Delete event
7be52756
PT
49 public void deleteEvent(String id) {
50 database.delete(MySQLiteHelper.Name, MySQLiteHelper.ID + "=" + id, null);
51 }
bdd8f09c 52 //Search event
7be52756
PT
53 //Get all events
54 public List<Event> getAllEvents() {
55 List<Event> events=new ArrayList<Event>();
56 Cursor cursor=database.query(MySQLiteHelper.Name,allColumns,null,null,null,null,null);
57
58 for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()) {
59 Event ev=cursorToEvent(cursor);
60 events.add(ev);
61 }
62 return events;
63 }
64
65 private Event cursorToEvent(Cursor cursor) {
66 // TODO Auto-generated method stub
67 Event e = new Event();
68 e._id=cursor.getString(0);
69 e.title=cursor.getString(1);
70 e.description=cursor.getString(2);
71 e.date=cursor.getInt(3);
72 e.duration=cursor.getInt(4);
73 if(cursor.getInt(5)==1) e.repeat=true;
74 else e.repeat=false;
75 e.repeatInterval=cursor.getInt(6);
76 e.location=cursor.getString(7);
77 return e;
78 }
bdd8f09c 79}
This page took 0.032883 seconds and 4 git commands to generate.