LoginSignup
22
22

More than 5 years have passed since last update.

ContentProviderをadbから直接実行するcontentコマンド

Last updated at Posted at 2014-10-10

from. Yukiの枝折 - Android:contentコマンドでContentProviderを呼び出す

Intro

Android4.1.1からadb shellからContentProviderへ直接リクエストを送信するcontentコマンドが追加された.
ContentProviderのテストや連携先ContentProviderの情報取得をadbから手軽に実行でき, コードを書く必要はない.

content command:

content query --uri content://yuki.authority/

contentコマンドはContentProviderへの命令をコマンドラインで指定・実行する.
ContentProviderがデータベースを扱うのであればレコードへのCRUD操作が実行されるといった具合に, contentコマンドの命令はアプリからのContentProvider呼び出しと同じで透過に扱える.

データベースの編集という用途ではSQLite3コマンドがベストな選択肢であるが, SQLite3コマンドの対象がデータベースなのに対して, contentコマンドの対象はContentProviderとなる.
これはつまり, ContentProviderの動作確認ツールとして使用可能であり, その透過性からContentProviderを実行してのContentObserverを確認すると行った用途にも使える.

Usage

help

contentコマンドの使用方法を調べるコマンド

adb shell content

query

ContentProviderのqueryを実行する.
引数についてはContentResolver.queryに渡す引数のそれと同じ感覚で使える.

adb shell content query --uri <URI> [--projection <PROJECTION>] [--where <WHERE>] [--sort <SORT_ORDER>]

insert

ContentProviderのinsertを実行する.

adb shell content insert --uri <URI> --bind <BINDING> [--bind <BINDING>...]

update

ContentProviderのupdateを実行する.

adb shell content update --uri <URI> [--where <WHERE>]

delete

ContentProviderのdeleteを実行する.

adb shell content delete --uri <URI> --bind <BINDING> [--bind <BINDING>...] [--where <WHERE>]

Arguments

共通するコマンド引数については下記.

URI

コンテンツプロバイダのURI

--uri content://hoge.authority

PROJECTION

検索対象のカラム名を指定する. 指定方法は

<COLUMN_NAME>[:<COLUMN_NAME>...]

column1:column2:column3

WHERE

SQLのWHERE句を指定します.

--where "column1='where_string'"

SORT_OREDER

ソート対象の列を指定します.

--sort "column1 DESC"

BINDING

次のフォーマットに従い, ContentValuesを作る感覚でカラムにバインドする情報を指定する.

<COLUMN_NAME>:<TYPE>:<COLUMN_VALUE>

TYPEには次の型が指定可能.

  • b : boolean
  • s : string
  • i : integer
  • l : long
  • f : float
  • d : double

column:s:new_string

Tests

動作確認の対象となるContentProviderとデータベーステーブルの情報は下記.

ContentProvider

Authority

yuki.authority

Table

Scheme

cid name type
0 _id INTEGER
1 number INTEGER

Contents

_id number
1 1416078507
2 1355288181
3 380734541
4 254152153
5 18301077

content commands

query

content query --uri content://yuki.authority/ --projection _id:number --where "_id BETWEEN 1 AND 3" --sort "_id DESC" _id:number --where "_id BETWEEN 1 AND 3" --sort "_id DESC"

Result:

Row: 0 _id=3, number=380734541
Row: 1 _id=2, number=1355288181
Row: 2 _id=1, number=1416078507

insert

content insert --uri content://yuki.authority/ --bind number:i:1234567 yuki.authority/ --bind number:i:1234567

Result: (insert結果をqueryで確認)

content query --uri content://yuki.authority/ --projection _id:number --where "number=1234567" --sort "_id DESC" _id:number --where "number=1234567" --sort "_id DESC"

Row: 0 _id=6, number=1234567

update

content update --uri content://yuki.authority/ --bind number:i:9999 yuki.authority/ --bind number:i:9999

Result: (update結果をqueryで確認)

content query --uri content://yuki.authority/ --projection _id:number --where "number=9999" _id:number --where "number=9999"

Row: 0 _id=1, number=9999
Row: 1 _id=2, number=9999
Row: 2 _id=3, number=9999
Row: 3 _id=4, number=9999
Row: 4 _id=5, number=9999
Row: 5 _id=6, number=9999

delete

content delete --uri content://yuki.authority/ --where "_id=1" yuki.authority/ --where "_id=1"

Result: (delete結果をqueryで確認)

content query --uri content://yuki.authority/ --projection _id:number com.example.testcprovider/ --projection _id:number

Row: 0 _id=2, number=9999
Row: 1 _id=3, number=9999
Row: 2 _id=4, number=9999
Row: 3 _id=5, number=9999
Row: 4 _id=6, number=9999

以上.

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