theme: apple-basic
layout: intro
highlighter: shiki
lineNumbers: true
| 이름 |
조작 |
SQL |
| Create |
생성 |
INSERT |
| Read(또는 Retrieve) |
읽기(또는 인출) |
SELECT |
| Update |
갱신 |
UPDATE |
| Delete(또는 Destroy) |
삭제(또는 파괴) |
DELETE |
// 작성자가 무명씨, 홍길동인 문서의 모든 항목을 가져옴
SELECT * FROM document WHERE author like '김*';
// table에 field1=value1, field2=value2, ...와 같은 속성값을 가지는 항목을 새로 생성하여 삽입
INSERT INTO table(field1, field2, ...) VALUES (value1, value2, ...);
// 데이터를 수정하는 구문으로 table의 field1에 value1, field2에 value2, ... 로 변경
UPDATE table SET field1=value1, field2=value2, {WHERE 조건};
// 데이터를 삭제
DELETE FROM table {WHERE 조건};
// 데이터베이스 내의 모든 테이블, 스키마, 관계(Relation)를 전부 삭제
DROP DATABASE database_name;
| Android API |
SQLite Version |
| API 31 |
3.32 |
| API 30 |
3.28 |
| API 28 |
3.22 |
| API 27 |
3.19 |
| API 26 |
3.18 |
| Classes |
Description |
| SQLiteClosable |
An object created from a SQLiteDatabase that can be closed. |
| SQLiteCursor |
A Cursor implementation that exposes results from a query on a SQLiteDatabase. |
| SQLiteDatabase |
Exposes methods to manage a SQLite database. |
| SQLiteOpenHelper |
A helper class to manage database creation and version management. |
| SQLiteProgram |
A base class for compiled SQLite programs. |
| SQLiteQuery |
Represents a query that reads the resulting rows into a SQLiteQuery. |
| SQLiteQueryBuilder |
This is a convenience class that helps build SQL queries to be sent to SQLiteDatabase objects. |
| SQLiteStatement |
Represents a statement that can be executed against a database. |
There is no compile-time verification of raw SQL queries. As your data graph changes, you need to update the affected SQL queries manually. This process can be time consuming and error prone.
You need to use lots of boilerplate code to convert between SQL queries and data objects.
ref) https://developer.android.com/training/data-storage/room
@Entity
data class User(
@PrimaryKey val uid: Int,
@ColumnInfo(name = "first_name") val firstName: String?,
@ColumnInfo(name = "last_name") val lastName: String?
)
@Dao
interface UserDao {
@Query("SELECT * FROM user")
fun getAll(): List<User>
@Query("SELECT * FROM user WHERE uid IN (:userIds)")
fun loadAllByIds(userIds: IntArray): List<User>
@Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
"last_name LIKE :last LIMIT 1")
fun findByName(first: String, last: String): User
@Insert
fun insertAll(vararg users: User)
@Delete
fun delete(user: User)
}
@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
- Less boilerplate code
- Compile-time checked queries
- Ease of implementing migrations
- High degree of testability
- Keep database work away from the main thread