LoginSignup
7
8

More than 5 years have passed since last update.

Android - OrmLiteを使った時のDatabaseHelperクラスサンプル

Last updated at Posted at 2013-03-04

OrmLiteを使った時のDatabaseHelperクラスサンプル

initメソッドは、アプリ内で初期化させたいときとかに使ってます。

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private Resources res;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        res = context.getResources();
    }

    @Override
    public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {

        try {
            TableUtils.dropTable(arg1, Book.class, true);
            TableUtils.dropTable(arg1, EvernoteNote.class, true);
            TableUtils.dropTable(arg1, Genre.class, true);
            TableUtils.dropTable(arg1, Series.class, true);
            TableUtils.dropTable(arg1, Author.class, true);
            TableUtils.dropTable(arg1, Image.class, true);
            TableUtils.createTable(arg1, Image.class);
            TableUtils.createTable(arg1, Author.class);
            TableUtils.createTable(arg1, Series.class);
            TableUtils.createTable(arg1, Genre.class);
            TableUtils.createTable(arg1, EvernoteNote.class);
            TableUtils.createTable(arg1, Book.class);
        } catch (java.sql.SQLException e) {
            Log.e(DatabaseHelper.class.getName(), res.getString(R.string.system_error_0001), e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource source, int oldVersion, int newVersion) {
        for (int i = oldVersion; i < newVersion; i++) {

            switch (i) {
            case 3:
                try {
                    Dao<Book, Integer> dao = getDao(Book.class);
                    dao.executeRaw("ALTER TABLE `book` ADD COLUMN `description` VARCHAR;");
                } catch (SQLException e) {
                    Log.e(DatabaseHelper.class.getName(), "book table description は既に追加済みです。", e);
                }
                break;
            case 4:
                try {
                    Dao<Author, Integer> author = getDao(Author.class);
                    author.executeRaw("ALTER TABLE `author` ADD COLUMN `cnt` INTEGER;");
                    Dao<Genre, Integer> genre = getDao(Genre.class);
                    genre.executeRaw("ALTER TABLE `genre` ADD COLUMN `cnt` INTEGER;");
                    Dao<Series, Integer> series = getDao(Series.class);
                    series.executeRaw("ALTER TABLE `series` ADD COLUMN `cnt` INTEGER;");
                } catch (SQLException e) {
                    Log.e(DatabaseHelper.class.getName(), "author,genre,series table cnt は既に追加済みです。", e);
                }
                break;
            default:
                break;
            }
        }
    }

    public void init() {
        try {
            TableUtils.dropTable(this.getConnectionSource(), Book.class, true);
            TableUtils.dropTable(this.getConnectionSource(), EvernoteNote.class, true);
            TableUtils.dropTable(this.getConnectionSource(), Genre.class, true);
            TableUtils.dropTable(this.getConnectionSource(), Series.class, true);
            TableUtils.dropTable(this.getConnectionSource(), Author.class, true);
            TableUtils.dropTable(this.getConnectionSource(), Image.class, true);
            TableUtils.createTable(this.getConnectionSource(), Image.class);
            TableUtils.createTable(this.getConnectionSource(), Author.class);
            TableUtils.createTable(this.getConnectionSource(), Series.class);
            TableUtils.createTable(this.getConnectionSource(), Genre.class);
            TableUtils.createTable(this.getConnectionSource(), EvernoteNote.class);
            TableUtils.createTable(this.getConnectionSource(), Book.class);
        } catch (java.sql.SQLException e) {
            Log.e(DatabaseHelper.class.getName(), res.getString(R.string.system_error_0001), e);
        }
    }
}
7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8