Advertisement
Guest User

DBAdapter (friend)

a guest
Dec 11th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package se.muradi.enayatullah.uppgift3;
  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.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10.  
  11. public class DBAdapter {
  12.     static final String KEY_ROWID = "id";
  13.     static final String KEY_DATUM = "datum";
  14.     static final String KEY_TYP = "typ";
  15.     static final String KEY_SUMMA = "summa";
  16.     static final String TAG = "DBAdapter";
  17.     static final String DATABASE_NAME = "BudgetDB";
  18.     static final String DATABASE_TABLE = "information";
  19.     static final int DATABASE_VERSION = 1;
  20.     static final String DATABASE_CREATE = "create table information (id integer primary key autoincrement, "
  21.             + "datum text not null, typ text not null, summa text not null);";
  22.     final Context context;
  23.     DatabaseHelper DBHelper;
  24.     SQLiteDatabase db;
  25.  
  26.     public DBAdapter(Context ctx) {
  27.         this.context = ctx;
  28.         DBHelper = new DatabaseHelper(context);
  29.     }
  30.  
  31.     private static class DatabaseHelper extends SQLiteOpenHelper {
  32.         DatabaseHelper(Context context) {
  33.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  34.         }
  35.  
  36.         @Override
  37.         public void onCreate(SQLiteDatabase db) {
  38.             try {
  39.                 db.execSQL(DATABASE_CREATE);
  40.             } catch (SQLException e) {
  41.                 e.printStackTrace();
  42.             }
  43.         }
  44.  
  45.         @Override
  46.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  47.             Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  48.                     + newVersion + ", which will destroy all old data");
  49.             db.execSQL("DROP TABLE IF EXISTS contacts");
  50.             onCreate(db);
  51.         }
  52.     }
  53.  
  54.     // ---opens the database---
  55.     public DBAdapter open() throws SQLException {
  56.         db = DBHelper.getWritableDatabase();
  57.         return this;
  58.     }
  59.  
  60.     // ---closes the database---
  61.     public void close() {
  62.         DBHelper.close();
  63.     }
  64.  
  65.     // ---insert a contact into the database---
  66.     public long insertContact(String datum, String typ, String summa) {
  67.         ContentValues initialValues = new ContentValues();
  68.         initialValues.put(KEY_DATUM, datum);
  69.         initialValues.put(KEY_TYP, typ);
  70.         initialValues.put(KEY_SUMMA, summa);
  71.         return db.insert(DATABASE_TABLE, null, initialValues);
  72.     }
  73.  
  74.     // ---deletes a particular contact---
  75.     public boolean deleteContact(long rowId) {
  76.         return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
  77.     }
  78.  
  79.     // ---retrieves all the contacts---
  80.     public Cursor getAllContacts() {
  81.         return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_DATUM,
  82.                 KEY_TYP, KEY_SUMMA }, null, null, null, null, null);
  83.     }
  84.  
  85.     // ---retrieves a particular contact---
  86.     public Cursor getContact(String typ) throws SQLException {
  87.         Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
  88.                 KEY_ROWID, KEY_DATUM, KEY_TYP, KEY_SUMMA }, KEY_TYP + "= '" + typ + "'",
  89.                 null, null, null, null, null);
  90.         if (mCursor != null) {
  91.             mCursor.moveToFirst();
  92.         }
  93.         return mCursor;
  94.     }
  95.  
  96.     // ---updates a contact---
  97.     public boolean updateContact(long rowId, String datum, String typ, String summa) {
  98.         ContentValues args = new ContentValues();
  99.         args.put(KEY_DATUM, datum);
  100.         args.put(KEY_TYP, typ);
  101.         args.put(KEY_SUMMA, summa);
  102.         return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement