]>
Commit | Line | Data |
---|---|---|
1 | package ro.ieval.unical; | |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.List; | |
5 | ||
6 | import android.content.ContentValues; | |
7 | import android.content.Context; | |
8 | import android.database.Cursor; | |
9 | import android.database.SQLException; | |
10 | import android.database.sqlite.SQLiteDatabase; | |
11 | ||
12 | public class DatabaseInteract { | |
13 | private SQLiteDatabase database; | |
14 | private MySQLiteHelper dbHelper; | |
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 | }; | |
21 | ||
22 | public DatabaseInteract(Context context) { | |
23 | dbHelper = new MySQLiteHelper(context); | |
24 | database=dbHelper.getWritableDatabase(); | |
25 | } | |
26 | ||
27 | public void open() throws SQLException { | |
28 | database=dbHelper.getWritableDatabase(); | |
29 | } | |
30 | ||
31 | public void close() { | |
32 | dbHelper.close(); | |
33 | } | |
34 | ||
35 | //Insert event | |
36 | public void insertEvent(Event e) { | |
37 | ContentValues values = new ContentValues(); | |
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); | |
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 | } | |
48 | //Delete event | |
49 | public void deleteEvent(String id) { | |
50 | database.delete(MySQLiteHelper.Name, MySQLiteHelper.ID + "=" + id, null); | |
51 | } | |
52 | //Search event | |
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 | } | |
79 | } |