LoginSignup
4
4

More than 5 years have passed since last update.

ORMのOllie使用方法

Last updated at Posted at 2015-12-27

ORMのOllie使用方法

Ollie

インストール

1.gradleの設定
 TOPレベルのbuild.gradleのdependenciesに
 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'を追加

 アプリレベルのbuild.gradleで
 apply plugin: 'com.android.application'の下に
 apply plugin: 'com.neenbedankt.android-apt'を追加
 dependenciesに下記を追加
 compile 'com.michaelpardo:ollie:0.3.1'
 provided 'com.michaelpardo:ollie-compiler:0.3.1'

2.DBの初期化
DBとversionを指定
java:MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//DB初期化
Ollie.with(getApplicationContext())
.setName("DBNAME")
.setVersion(1) //version
.setLogLevel(Ollie.LogLevel.FULL)
.init();
}

※Ollieの所でAlt + Enterでクラスをimportしてください

3.テーブルの作成
Modelクラスを継承してテーブルを作成する。

Recipient
@Table("recipient")
public class Recipient extends Model {

    //プライマリキーはAIの_id。自動で生成される

    @Column("name")
    public String name;

    @Column("email")
    public String email;

    @Column("is_delivery")
    public Integer is_delivery;
}

4.インサート
Modelのオブジェクトを生成して、値を設定しsave()

SecondActivity
        Recipient recipient   = new Recipient();
        recipient.name        = "tester";
        recipient.email       = "test@test.com";
        recipient.is_delivery = 1;
        recipient.save();

5.レコードの取得
fromの所にテーブルのクラス名を設定する。

SecondActivity
        Recipient recipientData = Select.from(Recipient.class).fetchSingle();
        Log.d("dbData", recipientData.email);

導入が簡単♪
随時更新予定
→複数の検索条件の時にwhereメソッドを連続で使用できないっぽいので、DBFlowに乗り換えます。
DBFlowはこちら↓
http://qiita.com/kiimiiis/items/603524744f312a24e3f0

4
4
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
4
4