Androidでは端末内でデータを永続化するときに、たくさんのデータを保存する場合はSQLiteデータベースを使用すべきです。
SQLiteデータベースは各アプリケーションの固有保存領域に作成されるため作成したアプリケーションしかアクセスすることができません。
他のアプリケーションにデータベースを公開する場合は
コンテンツ プロバイダの仕組みを使用する必要があります。
SQLite関連のクラスは
android.database.sqliteパッケージに含まれています。
また、AndroidOSのバージョンのによってSQLiteのバージョンが異なるので注意しましょう。
| Android API | SQLite Version | 
|---|
| API 24 | 3.9 | 
| API 21 | 3.8 | 
| API 11 | 3.7 | 
| API 8 | 3.6 | 
| API 3 | 3.5 | 
| API 1 | 3.4 | 
SQLiteを導入する準備
Androidプロジェクトに何も考えずにSQLiteを導入すると、
ActivityにDBへのアクセス処理が記述され無駄にActivityが太ってしまいます。
そこで、SQLiteを導入するにあたり保守性や可読性を高めるための下準備を行います。
導入準備
関連クラス
SQLiteDatabaseに定義されているコンビニエンスメソッドを利用する
SQLiteDatabaseには単純なSQL文を実行するコンビニエンスメソッドが多数用意されています。
テーブル名といくつかの条件を引数を渡すだけで使えるので、積極的に使用しましょう。
INSERT文を使う
SELECT文を使う
UPDATE文を使う
DELETE文を使う
REPLACE文を使う
関連クラス
未整理のクラスやインターフェース
| SQLiteCursorDriver | A driver for SQLiteCursors that is used to create them and gets notified by the cursors it creates on significant events in their lifetimes. | 
| SQLiteTransactionListener | A listener for transaction events. | 
| SQLiteClosable | An object created from a SQLiteDatabase that can be closed. | 
| SQLiteProgram | A base class for compiled SQLite programs. | 
| SQLiteQuery | Represents a query that reads the resulting rows into a SQLiteQuery. | 
| SQLiteQueryBuilder | This is a convience class that helps build SQL queries to be sent to SQLiteDatabase objects. | 
| SQLiteStatement | Represents a statement that can be executed against a database. | 
| SQLiteAbortException | An exception that indicates that the SQLite program was aborted. | 
| SQLiteAccessPermException | This exception class is used when sqlite can’t access the database file due to lack of permissions on the file. | 
| SQLiteBindOrColumnIndexOutOfRangeException | Thrown if the the bind or column parameter index is out of range | 
| SQLiteBlobTooBigException |  | 
| SQLiteCantOpenDatabaseException |  | 
 
| SQLiteConstraintException | An exception that indicates that an integrity constraint was violated. | 
| SQLiteDatabaseCorruptException | An exception that indicates that the SQLite database file is corrupt. | 
| SQLiteDatabaseLockedException | Thrown if the database engine was unable to acquire the database locks it needs to do its job. | 
| SQLiteDatatypeMismatchException |  | 
| SQLiteDiskIOException | An exception that indicates that an IO error occured while accessing the SQLite database file. | 
| SQLiteDoneException | An exception that indicates that the SQLite program is done. | 
| SQLiteException | A SQLite exception that indicates there was an error with SQL parsing or execution. | 
| SQLiteFullException | An exception that indicates that the SQLite database is full. | 
| SQLiteMisuseException | This error can occur if the application creates a SQLiteStatement object and allows multiple threads in the application use it at the same time. | 
| SQLiteOutOfMemoryException |  | 
| SQLiteReadOnlyDatabaseException |  | 
| SQLiteTableLockedException |  |