キーと値を追加

広告

「ContentValues」クラスのオブジェクトには複数のキーと値のペアを追加する事ができます。カラムにはカラム名を指定しますが、カラムは様々なデータ型がありますので各データ型に対応したメソッドが用意されています。「ContentValues」クラスで用意されている「put」メソッドを使います。

Integer:

Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Short:

Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Byte:

Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

String:

Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Long:

Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Boolean:

Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Float:

Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Double:

Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

各メソッドの1番目の引数にはカラム名を表すStringクラスの値を指定します。そして2番目の引数にはカラムに設定されているデータ型に対応したクラスの値を指定します。

「ContentValues」クラスのオブジェクトには複数のペアを追加できます。例えばテーブルにカラムが2つ含まれていて、テーブルにデータを追加したい場合にはカラムの数だけ「put」メソッドを実行しておきます。

具体的には次のように記述します。

ContentValues cv = new ContentValues();
cv.put("name", "orange");
cv.put("price", 100);

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。