Advertisement
Guest User

SectionsDataSource.java

a guest
Jul 20th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package com.mysite.datasources;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.SQLException;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.util.Log;
  9.  
  10. import com.mysite.models.Section;
  11. import com.mysite.utilities.DateHelper;
  12. import com.mysite.utilities.DbHelper;
  13.  
  14. public class SectionsDataSource
  15. {
  16.  
  17.     //private Context mContext;
  18.    
  19.     // Database fields
  20.     private SQLiteDatabase database;
  21.     private DbHelper dbHelper;
  22.    
  23.     public SectionsDataSource(Context context)
  24.     {
  25.         //this.mContext = context;
  26.         dbHelper = new DbHelper(context);
  27.     }
  28.    
  29.     public void open() throws SQLException
  30.     {
  31.         Log.d("mysite", "SectionsDataSource.open();");
  32.         database = dbHelper.getWritableDatabase();
  33.     }
  34.    
  35.     public void close()
  36.     {
  37.         Log.d("mysite", "SectionsDataSource.close();");
  38.         dbHelper.close();
  39.     }
  40.    
  41.     public void updated(String slug) {
  42.         this.open();
  43.         try {
  44.             Section s1 = new Section();
  45.             s1.last_updated = DateHelper.now();
  46.            
  47.             ContentValues values = new ContentValues();
  48.             values.put(Section.COLUMN_LASTUPDATED, s1.last_updated);
  49.             database.update(Section.TABLE, values, Section.COLUMN_SLUG + " = ?", new String[] { slug });
  50.         } catch (Exception e) {
  51.             System.out.println(e);
  52.         }
  53.         finally
  54.         {
  55.         this.close();
  56.         }
  57.     }
  58.    
  59.     public String last_updated(String slug) {
  60.         this.open();
  61.         Section s1 = new Section();
  62.         try {
  63.             Cursor cursor = database.query(Section.TABLE,
  64.                     new String[] { Section.COLUMN_LASTUPDATED },
  65.                     Section.COLUMN_SLUG + "=?",
  66.                     new String[] {slug},
  67.                     null, null, null, null);
  68.            
  69.             cursor.moveToFirst(); // ERRORING HERE - didn't return anything?
  70.             s1 = cursorToSection(cursor);
  71.             cursor.close();
  72.            
  73.         } catch (Exception e) {
  74.             e.printStackTrace();
  75.             s1.last_updated = "";
  76.         }
  77.         finally
  78.         {
  79.         this.close();
  80.         }
  81.         return s1.last_updated;
  82.     }
  83.  
  84.    
  85.     private Section cursorToSection(Cursor cursor)
  86.     {
  87.         Section section = new Section();
  88.         //section.id = cursor.getLong(cursor.getColumnIndex(Section.COLUMN_ID));
  89.         //section.title = cursor.getString(cursor.getColumnIndex(Section.COLUMN_TITLE));
  90.         section.last_updated = cursor.getString(cursor.getColumnIndex(Section.COLUMN_LASTUPDATED));
  91.         return section;
  92.     }
  93.    
  94.     /*private ContentValues buildContentValues(Section section)
  95.     {
  96.         ContentValues values = new ContentValues();
  97.         values.put(Section.COLUMN_TITLE, section.title);
  98.         values.put(Section.COLUMN_LASTUPDATED, section.last_updated);
  99.         return values;
  100.     }*/
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement