LoginSignup
1
0

More than 1 year has passed since last update.

sqliteの属性について

Posted at

目的

US版のMicrosoftのドキュメントをあさる手間を考え備忘録として記載。

参考

下記を参考に記事を記載しています。
(JP版)
https://learn.microsoft.com/ja-jp/xamarin/android/data-cloud/data-access/using-sqlite-orm
(US版)
https://learn.microsoft.com/en-us/xamarin/android/data-cloud/data-access/using-sqlite-orm

属性の使い方

下記のようにテーブルにするクラスのメンバ変数に設定する。

using SQLite;
namespace sqliteDemo {
    // テーブルの物理名を設定
    [Table ( "DemoTable" )]
    public class DemoTable {
        // カラムの属性を設定
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        [MaxLength ( 250 ), Unique]
        public string Name { get; set; }
    }
}

属性の種類

PrimaryKey

主キーの設定。
複合主キーはサポートされないとのこと。(参照のmicrosoft公式ドキュメントより)

[PrimaryKey]
public int id { get; set; }

AutoIncrement

データベースに挿入されるたびに該当するカラムがインクリメントされる。

[AutoIncrement]
public int id { get; set; }

Column

カラムの物理名を指定する。

[Column("test_id")]
public int id { get; set; }

Table

テーブルの物理名を指定する。

[Table ( "DEMO_TABLE" )]
public class DemoTable {
…
}

MaxLength

カラムのサイズを指定する。

[MaxLength(10)]
public int id { get; set; }

Ignore

データベースに挿入されないフィールドとする。

[Ignore]
public int hidden_id { get; set; }

Unique

指定されたカラムのデータを一意とする。

[Unique]
public int id { get; set; }

複数設定

一つのカラムに複数設定することも可能

[PrimaryKey, AutoIncrement,Unique]
public int id { get; set; }
1
0
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
1
0