๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ป Computer/Android

SQLite Database ์™€ Content provider

1. SQLite Database๋Š” SQLite OpenHelper ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ DBhelper๋ฅผ ํ†ตํ•ด ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ์ƒ์„ฑํ•œ๋‹ค.

*์ด๋•Œ ํ…Œ์ด๋ธ”์— ์‚ฌ์šฉ๋  ํ…Œ์ด๋ธ”๋ช…, column๋ช…๋“ค์€ Constant Class๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ๊ด€๋ฆฌํ•œ๋‹ค.

 

SQLite OpenHelper Public Methods

- abstract void onCreate (SQLiteDatabase db)

  : ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์ฝ”๋“œ -> executeSQL(ํ…Œ์ด๋ธ”๋ช…)

 

- abstract void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)

  : ๊ตฌ๋ฒ„์ „ db์™€ ์‹ ๋ฒ„์ „ db์™€ ๋น„๊ต ํ›„ ์—…๋ฐ์ดํŠธ

 

- void onOpen (SQLiteDatabase db)

  : ์ƒ์„ฑ ๋ฐ ์—…๋ฐ์ดํŠธ ์ดํ›„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์—ด๋ ธ์„ ๋•Œ ํ˜ธ์ถœ

 

2. Content Provider (์•ฑ๊ณผ ์•ฑ ์ €์žฅ์†Œ ์‚ฌ์ด์—์„œ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ์„ ์‰ฝ๊ฒŒ ํ•˜๋„๋ก ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ํด๋ž˜์Šค)

- String์œผ๋กœ Schema์™€ Authority์„ค์ •, Uri๋กœ path์„ค์ •

- UriMatcher๋กœ contentProvider๋กœ ๋“ค์–ด์˜ฌ ์š”์ฒญ๋“ค์„ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด์„œ addURI()

- content Uri ์„ค๊ณ„ : https://developer.android.com/guide/topics/providers/content-provider-creating?hl=ko#ContentURI

  uriMatcher.addURI(authority, path, constant)
authority content provider ์ค‘ ์–ด๋Š authority์— ํ•ด๋‹นํ•˜๋Š” ์ง€ ๊ตฌ๋ถ„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํƒœ๊ทธ
path authority ์ค‘ ํ•ด๋‹นํ•˜๋Š” path
constant (=code) match() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ์‹œ ์ผ์น˜ํ•˜๋ฉด ๋ฐ˜ํ™˜๋˜๋Š” ์ •์ˆ˜
Content provider

com.example.app.provider๋ผ๋Š”
๊ถŒํ•œ์ด ์žˆ์Œ
content๊ฐ€ ํ•„์š”ํ•œ ๊ณณ

content://com.example.app.provider/table์ด๋ผ๋Š” content uri๋ฅผ ๊ฐ€์ง

- content provider๋Š” ๊ทœ์น™์— ๋”ฐ๋ผ uri ๋งจ๋์— ํ–‰์— ๋Œ€ํ•œ ID๊ฐ’์ด ํฌํ•จ๋œ content uri๋ฅผ ํ—ˆ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ”์˜ ๋‹จ์ผ ํ–‰์— ๋Œ€ํ•œ ์—‘์„ธ์Šค๋ฅผ ์ œ๊ณต

์•ฑ์ด ์ œ๊ณต์ž์— ๋Œ€ํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , cursorAdapter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ cursor๋ฅผ listview์— ํ‘œ์‹œํ•จ

 

Content Provider Methods

- onCreate()

 : dbhelper ์„ ์–ธ

 

- insert (Uri uri, ContentValues values) // pour Writable

 : ๊ณ„์† ์“ฐ๊ธฐ ์œ„ํ•ด final๋กœ SQLiteDatabase๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  db์— ์ ‘๊ทผํ•˜์—ฌ ์ •๋ณด insert

 

- query (Uri, uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) // pour Readable

 : ๊ณ„์† ์“ฐ๊ธฐ ์œ„ํ•ด final๋กœ SQLiteDatabase๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , Cursor๋ฅผ ๋ฆฌํ„ด๋ฐ›์„ ๋ณ€ใ…Ž์ˆ˜๋กœ ์„ ์–ธ

*query๋Š” ์ฝ์–ด์˜ค๊ธฐ ์ž‘์—…์ด๊ณ  ์˜ค๋ž˜๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— background thread๋‚˜ asynctask์—์„œ ์“ฐ๋Š” ๊ฒŒ ์ข‹์Œ

 

3. Content Resolver (Content Provider์˜ ์ฃผ์†Œ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ด)

Methode

- query (uri, projection, selection, selectionArgs, sortOrder)

> uri : content://schema ๋ฐฉ์‹์˜ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ์ฃผ์†Œ

> projection : ๊ฐ€์ ธ์˜ฌ column ์ด๋ฆ„ ๋ชฉ๋ก, null์ด๋ฉด ๋ชจ๋“  column

> selection : where์ ˆ์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ

> selectionArgs : selection์—์„œ ?๋กœ ํ‘œ์‹œํ•œ ๊ณณ์— ๋“ค์–ด๊ฐˆ ๋ฐ์ดํ„ฐ

> sortOrder : ์ •๋ ฌ์„ ์œ„ํ•œ order by ๊ตฌ๋ฌธ

>>> sql๋ฌธํ•˜๊ณ  ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ปค์„œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฐพ์Œ

ex) Cursor cursor = getContentResolver().query( ~~~ )

 

 

์ฐธ๊ณ ์‚ฌ์ดํŠธ

developer.android.com/guide/topics/providers/content-provider-creating

shnoble.tistory.com/62

blog.yena.io/studynote/2017/11/11/Android-Content-Provider.html

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•