DataHelper.java :  » App » dsa-dices-android » at » barbande » dsadices » persistance » Android Open Source

Android Open Source » App » dsa dices android 
dsa dices android » at » barbande » dsadices » persistance » DataHelper.java
package at.barbande.dsadices.persistance;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import at.barbande.dsadices.presentation.R;

import java.util.ArrayList;
import java.util.List;

public class DataHelper {

  private static final String DATABASE_NAME = "character.db";
  private static final int DATABASE_VERSION = 1;
  private static final String TABLE_NAME = "table1";

  private Context context;
  private SQLiteDatabase db;

  private SQLiteStatement insertStmt;
  private static final String INSERT = "insert into " + TABLE_NAME + "(name) values (?)";

  public DataHelper(Context context) {
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
    this.insertStmt = this.db.compileStatement(INSERT);
  }

  public long insert(String name) {
    this.insertStmt.bindString(1, name);
    return this.insertStmt.executeInsert();
  }

  public void deleteAll() {
    this.db.delete(TABLE_NAME, null, null);
  }

  public List<String> selectAll() {
    List<String> list = new ArrayList<String>();
    Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, null, null, null, null, "name desc");
    if (cursor.moveToFirst()) {
      do {
        list.add(cursor.getString(0));
      } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
      cursor.close();
    }
    return list;
  }

  private static class OpenHelper extends SQLiteOpenHelper {

    private String createString;

    OpenHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
      String[] talents = context.getResources().getStringArray(R.array.talents);
      StringBuffer buf = new StringBuffer();
      for (int i = 0; i < talents.length; i++) {
        String talent = talents[i].split("\\(")[0];
        talent = talent.trim();
        talent = talent.replace(" ", "_");
        buf.append(", ");
        buf.append(talent);
        buf.append(" TEXT");
      }
      this.createString = buf.toString();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL("CREATE TABLE " + TABLE_NAME + "(name TEXT PRIMARY KEY" + this.createString + ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w("Example", "Upgrading database, this will drop tables and recreate.");
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
      onCreate(db);
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.