Android Open Source - on-the-hook Database Helper






From Project

Back to project page on-the-hook.

License

The source code is released under:

MIT License

If you think the Android project on-the-hook listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.yoandinkov.onthehook.db;
/*w  w w.jav  a  2 s.com*/
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.yoandinkov.onthehook.db.models.FishDbModel;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

  private static final String DATABASE_NAME = "Fishes.sqlite";

  private static final int DATABASE_VERSION = 1;

  private Dao<FishDbModel, Integer> fishDao = null;

  public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database,
      ConnectionSource connectionSource) {
    try {
      TableUtils.createTable(connectionSource, FishDbModel.class);
    } catch (SQLException e) {
      Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
      throw new RuntimeException(e);
    } catch (java.sql.SQLException e) {
      e.printStackTrace();
    }
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
      int oldVersion, int newVersion) {
    try {
      List<String> allSql = new ArrayList<String>();
      switch (oldVersion) {
      case 1:
        // allSql.add("alter table AdData add column `new_col` VARCHAR");
        // allSql.add("alter table AdData add column `new_col2` VARCHAR");
      }
      for (String sql : allSql) {
        db.execSQL(sql);
      }
    } catch (SQLException e) {
      Log.e(DatabaseHelper.class.getName(), "exception during onUpgrade",
          e);
      throw new RuntimeException(e);
    }

  }

  public Dao<FishDbModel, Integer> getFishesListDao() {
    if (null == fishDao) {
      try {
        fishDao = getDao(FishDbModel.class);
      } catch (java.sql.SQLException e) {
        e.printStackTrace();
      }
    }
    return fishDao;
  }

  public void dropFishesTable() {
    try {
      TableUtils.dropTable(getConnectionSource(), FishDbModel.class,
          false);
    } catch (java.sql.SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}




Java Source Code List

com.yoandinkov.onthehook.DraughtActivity.java
com.yoandinkov.onthehook.FishActivity.java
com.yoandinkov.onthehook.FishingActivity.java
com.yoandinkov.onthehook.LoginActivity.java
com.yoandinkov.onthehook.MainMenuActivity.java
com.yoandinkov.onthehook.RegisterActivity.java
com.yoandinkov.onthehook.RequestActivity.java
com.yoandinkov.onthehook.adapters.FishAdapter.java
com.yoandinkov.onthehook.crypt.JsonConverter.java
com.yoandinkov.onthehook.crypt.SHA1Converter.java
com.yoandinkov.onthehook.db.DatabaseHelper.java
com.yoandinkov.onthehook.db.DatabaseManager.java
com.yoandinkov.onthehook.db.models.FishDbModel.java
com.yoandinkov.onthehook.models.Coordinates.java
com.yoandinkov.onthehook.models.Credentials.java
com.yoandinkov.onthehook.models.FishButton.java
com.yoandinkov.onthehook.models.FishSpecies.java
com.yoandinkov.onthehook.models.Fish.java
com.yoandinkov.onthehook.models.Fishing.java
com.yoandinkov.onthehook.models.HttpResponseWrapper.java
com.yoandinkov.onthehook.models.HttpType.java
com.yoandinkov.onthehook.models.Login.java
com.yoandinkov.onthehook.models.NewFishing.java
com.yoandinkov.onthehook.models.Registration.java
com.yoandinkov.onthehook.models.RequestPurpose.java
com.yoandinkov.onthehook.models.Response.java
com.yoandinkov.onthehook.models.WaterLocation.java