Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.muradi.enayatullah.uppgift3;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- public class DBAdapter {
- static final String KEY_ROWID = "id";
- static final String KEY_DATUM = "datum";
- static final String KEY_TYP = "typ";
- static final String KEY_SUMMA = "summa";
- static final String TAG = "DBAdapter";
- static final String DATABASE_NAME = "BudgetDB";
- static final String DATABASE_TABLE = "information";
- static final int DATABASE_VERSION = 1;
- static final String DATABASE_CREATE = "create table information (id integer primary key autoincrement, "
- + "datum text not null, typ text not null, summa text not null);";
- final Context context;
- DatabaseHelper DBHelper;
- SQLiteDatabase db;
- public DBAdapter(Context ctx) {
- this.context = ctx;
- DBHelper = new DatabaseHelper(context);
- }
- private static class DatabaseHelper extends SQLiteOpenHelper {
- DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- try {
- db.execSQL(DATABASE_CREATE);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
- + newVersion + ", which will destroy all old data");
- db.execSQL("DROP TABLE IF EXISTS contacts");
- onCreate(db);
- }
- }
- // ---opens the database---
- public DBAdapter open() throws SQLException {
- db = DBHelper.getWritableDatabase();
- return this;
- }
- // ---closes the database---
- public void close() {
- DBHelper.close();
- }
- // ---insert a contact into the database---
- public long insertContact(String datum, String typ, String summa) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_DATUM, datum);
- initialValues.put(KEY_TYP, typ);
- initialValues.put(KEY_SUMMA, summa);
- return db.insert(DATABASE_TABLE, null, initialValues);
- }
- // ---deletes a particular contact---
- public boolean deleteContact(long rowId) {
- return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
- }
- // ---retrieves all the contacts---
- public Cursor getAllContacts() {
- return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_DATUM,
- KEY_TYP, KEY_SUMMA }, null, null, null, null, null);
- }
- // ---retrieves a particular contact---
- public Cursor getContact(String typ) throws SQLException {
- Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
- KEY_ROWID, KEY_DATUM, KEY_TYP, KEY_SUMMA }, KEY_TYP + "= '" + typ + "'",
- null, null, null, null, null);
- if (mCursor != null) {
- mCursor.moveToFirst();
- }
- return mCursor;
- }
- // ---updates a contact---
- public boolean updateContact(long rowId, String datum, String typ, String summa) {
- ContentValues args = new ContentValues();
- args.put(KEY_DATUM, datum);
- args.put(KEY_TYP, typ);
- args.put(KEY_SUMMA, summa);
- return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement