title: Android sql creating database once
body: <p>I want to create the database and all the data ONCE and update some data maybe later when needed. Throughout most of the tutorials I have read, they use two classes: one that extends SQLiteOpenHelper and another to simplify manipulating data. For example:</p>
  
  <pre><code>public class EventsData extends SQLiteOpenHelper {
  
  private static final String DATABASE_NAME = "events.db";
  private static final int DATABASE_VERSION = 1;
  private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INT, " + TITLE + " TEXT, " + UNLOCK + " INT)";
  private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS " + TABLE_NAME;
  
  public EventsData(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
      db.execSQL(DATABASE_CREATE);
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // TODO Auto-generated method stub
      db.execSQL(DATABASE_UPGRADE);
      onCreate(db);
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public class DataHelper {
  
  private static final String[] FROM = {ID, TITLE, UNLOCK};
  //private static final String ORDER_BY = UNLOCK + " DESC";
  
  private Context context;
  private SQLiteDatabase db;
  private EventsData eventsData;
  
  public DataHelper(Context context) {
      this.context = context;
      eventsData = new EventsData(this.context);
  }
  
  public void addEvent(int id, String name, int unlocked) {
      db = eventsData.getWritableDatabase();
      ContentValues cv = new ContentValues();
      cv.put(ID, id);
      cv.put(TITLE, name);
      cv.put(UNLOCK, unlocked);
      db.insertOrThrow(TABLE_NAME, null, cv);
  }
  
  public void updateEvent(int id, String name, int unlocked) {
      db = eventsData.getWritableDatabase();
      ContentValues cv = new ContentValues();
      cv.put(ID, id);
      cv.put(TITLE, name);
      cv.put(UNLOCK, unlocked);
      db.update(TABLE_NAME, cv, ID + "=" + id, null);
  }
  
  public Cursor getEvent() {
      db = eventsData.getReadableDatabase();
      Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, null);
      return cursor;
  }
  
  public StringBuilder showEvent(Cursor cursor) {
      StringBuilder sb = new StringBuilder();
      while (cursor.moveToNext()) {
          String id = cursor.getString(0);
          String title = cursor.getString(1);
          String unlock = cursor.getString(2);
          sb.append(id).append(": ");
          sb.append(unlock).append(": ");
          sb.append(title).append("\n");
      }
      return sb;
  }
  
  public void deleteAll() {
      db = eventsData.getWritableDatabase();
      db.delete(TABLE_NAME, null, null);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>In my main activity, I create an instance of DataHelper and call addEvent() to create and add data to the database. But everytime the application starts, it's going to "reset" all the data even if I were to call updateEvent().</p>