Use arguments for TrivialFragment
[unical.git] / src / ro / ieval / unical / DatabaseInteract.java
index a4d2580026880edac0a26896f6e3d77727b0345b..520eba8c7c222e7686069117ca49867721671b2b 100644 (file)
@@ -1,15 +1,27 @@
 package ro.ieval.unical;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import android.content.ContentValues;
 import android.content.Context;
+import android.database.Cursor;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 
 public class DatabaseInteract {
        private SQLiteDatabase database;
        private MySQLiteHelper dbHelper;
+       private String[] allColumns = {
+                       MySQLiteHelper.ID, MySQLiteHelper.title, 
+                       MySQLiteHelper.description, MySQLiteHelper.date,
+                       MySQLiteHelper.duration, MySQLiteHelper.repeat,
+                       MySQLiteHelper.repeatIn, MySQLiteHelper.location
+       };
        
        public DatabaseInteract(Context context) {
                dbHelper = new MySQLiteHelper(context);
+               database=dbHelper.getWritableDatabase();
        }
        
        public void open() throws SQLException {
@@ -20,7 +32,48 @@ public class DatabaseInteract {
                dbHelper.close();
        }
        
-       //Add event
+       //Insert event
+       public void insertEvent(Event e) {
+               ContentValues values = new ContentValues();
+               values.put(allColumns[0], e._id);
+               values.put(allColumns[1], e.title);
+               values.put(allColumns[2], e.description);
+               values.put(allColumns[3], e.date);
+               values.put(allColumns[4], e.duration);
+               values.put(allColumns[5], e.repeat);
+               values.put(allColumns[6], e.repeatInterval);
+               values.put(allColumns[7], e.location);
+               database.insert(MySQLiteHelper.Name, null, values);
+       }
        //Delete event
+       public void deleteEvent(String id) {
+               database.delete(MySQLiteHelper.Name, MySQLiteHelper.ID + "=" + id, null);
+       }
        //Search event
+       //Get all events
+       public List<Event> getAllEvents() {
+               List<Event> events=new ArrayList<Event>();
+               Cursor cursor=database.query(MySQLiteHelper.Name,allColumns,null,null,null,null,null);
+               
+               for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()) {
+                       Event ev=cursorToEvent(cursor);
+                       events.add(ev);
+               }
+               return events;
+       }
+
+       private Event cursorToEvent(Cursor cursor) {
+               // TODO Auto-generated method stub
+               Event e = new Event();
+               e._id=cursor.getString(0);
+               e.title=cursor.getString(1);
+               e.description=cursor.getString(2);
+               e.date=cursor.getInt(3);
+               e.duration=cursor.getInt(4);
+               if(cursor.getInt(5)==1) e.repeat=true;
+               else e.repeat=false;
+               e.repeatInterval=cursor.getInt(6);
+               e.location=cursor.getString(7);
+               return e;
+       }
 }
This page took 0.010654 seconds and 4 git commands to generate.