質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

1813閲覧

Room SQLCipher PRAGMA keyの記述方法がわからない

pice

総合スコア409

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2021/03/07 09:08

編集2021/03/07 12:05

Roomで動的に生成したクエリ(SQL文)を扱う方法 - Qiita@RawQueryを使用すると実装でsきそうですが、実際に、PRAGMA keyを指定したい場合の記述方法がわからず、質問させて頂きました。不躾なご質問で恐縮ですが、よろしくお願いします。

ViewModelは以下です。

kotlin

1class RequestDocumentsViewModel(application: Application): AndroidViewModel(application) { 2 3 private val requestDocumentsPersonInfoDao: RequestDocumentsPersonInfoDao 4 5 6 init { 7 val query = SimpleSQLiteQuery("PRAGMA key = 'passphrase'") 8 val db = RequestDocumentsPersonInfoDatabase.buildDatabase(application) 9 requestDocumentsPersonInfoDao = db.requestDocumentsPersonInfoDao() 10 11 12 } 13 14 val personData = requestDocumentsPersonInfoDao.loadAllRequestDocumentsPersonInfo() 15 val personDataAll = requestDocumentsPersonInfoDao.loadAllRequestDocumentsPersonInfoLiveData() 16 17 18 19 fun insert(name: String, phoneticGuides: String, birthday: String, mailAddress: String, phoneNumber: String) { 20 viewModelScope.launch(Dispatchers.IO) { 21 requestDocumentsPersonInfoDao.saveRequestDocumentsPersonInfo( 22 RequestDocumentsPersonInfo( 23 id = 0, 24 name = name, 25 phoneticGuides = phoneticGuides, 26 birthday = birthday, 27 mailAddress = mailAddress, 28 phoneNumber = phoneNumber, 29 zipStr = "", 30 prefecture = "", 31 city = "", 32 address = "", 33 disableCertifidate = "", 34 sendingMaterials = "", 35 remarks = "" 36 ) 37 ) 38 } 39 } 40 41 fun updateAddress(zipStr: String, prefecture: String, city: String, address: String) { 42 viewModelScope.launch (Dispatchers.IO) { 43 val editPersonInfo = personData.first() 44 editPersonInfo.zipStr = zipStr 45 editPersonInfo.prefecture = prefecture 46 editPersonInfo.city = city 47 editPersonInfo.address = address 48 requestDocumentsPersonInfoDao.updateRequestDocumentsPersonInfo(editPersonInfo) 49 } 50 } 51 52 fun updateParam(disableCertificate: String, sendingMaterials: String, remarks: String) { 53 viewModelScope.launch (Dispatchers.IO) { 54 val editPersonInfo = personData.first() 55 editPersonInfo.disableCertifidate = disableCertificate 56 editPersonInfo.sendingMaterials = sendingMaterials 57 editPersonInfo.remarks = remarks 58 } 59 } 60 61}

Entityは以下です。

kotlin

1import androidx.room.ColumnInfo 2import androidx.room.Entity 3import androidx.room.PrimaryKey 4 5@Entity(tableName = "request_documents_person_info") 6data class RequestDocumentsPersonInfo ( 7 @PrimaryKey(autoGenerate = true) 8 @ColumnInfo(name = "id") 9 val id: Int, 10 @ColumnInfo(name = "name") 11 val name : String, 12 @ColumnInfo(name="phoneticGuides") 13 val phoneticGuides: String, 14 @ColumnInfo(name="birthday") 15 val birthday: String, 16 @ColumnInfo(name="zipStr") 17 var zipStr: String, 18 @ColumnInfo(name="prefecture") 19 var prefecture: String, 20 @ColumnInfo(name="city") 21 var city: String, 22 @ColumnInfo(name="address") 23 var address :String, 24 @ColumnInfo(name="phoneNumber") 25 val phoneNumber: String, 26 @ColumnInfo(name="mailAddress") 27 val mailAddress: String, 28 @ColumnInfo(name="disableCertificate") 29 var disableCertifidate: String, 30 @ColumnInfo(name="sendingMaterials") 31 var sendingMaterials : String, 32 @ColumnInfo(name="remarks") 33 var remarks: String 34)

Daoクラスが以下です。

kotlin

1import androidx.lifecycle.LiveData 2import androidx.room.* 3 4@Dao 5interface RequestDocumentsPersonInfoDao { 6 7 8 // データ取得メソッド 9 @Query("SELECT * FROM request_documents_person_info") 10 fun loadAllRequestDocumentsPersonInfo(): List<RequestDocumentsPersonInfo> 11 12 @Query("SELECT * FROM request_documents_person_info") 13 fun loadAllRequestDocumentsPersonInfoLiveData(): LiveData<List<RequestDocumentsPersonInfo>> 14 15 // 挿入メソッド 16 @Insert(onConflict = OnConflictStrategy.REPLACE) 17 fun saveRequestDocumentsPersonInfo(vararg requestDocumentsPersonInfo: RequestDocumentsPersonInfo) 18 19 // 更新メソッド 20 @Update(onConflict = OnConflictStrategy.REPLACE) 21 fun updateRequestDocumentsPersonInfo(requestDocumentsPersonInfo: RequestDocumentsPersonInfo) 22 23 // 削除メソッド 24 @Delete 25 fun deleteRequestDocumentsPersonInfo(requestDocumentsPersonInfo: RequestDocumentsPersonInfo) 26 27}

Databaseクラスが以下です。

kotlin

1import android.content.Context 2import androidx.room.Database 3import androidx.room.Room 4import androidx.room.RoomDatabase 5import net.sqlcipher.database.SQLiteDatabase 6import net.sqlcipher.database.SupportFactory 7 8@Database(entities = [RequestDocumentsPersonInfo::class], version = 2, exportSchema = false) 9abstract class RequestDocumentsPersonInfoDatabase : RoomDatabase(){ 10 abstract fun requestDocumentsPersonInfoDao(): RequestDocumentsPersonInfoDao 11 12 companion object { 13 fun buildDatabase(context: Context): RequestDocumentsPersonInfoDatabase { 14 return Room.databaseBuilder( 15 context, 16 RequestDocumentsPersonInfoDatabase::class.java, 17 "request_documents_person_info.db" 18 ).openHelperFactory(SupportFactory(SQLiteDatabase.getBytes("PassPhrase".toCharArray()))) 19 .build() 20 } 21 } 22}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

PRAGMA Keyを使わなくても、Activity側で

kotlin

1val list = requestDocumentsViewModel.personData

とすることでレコードが取得でき、

kotlin

1list.count()

とすることで、レコードの件数も取得できました。

投稿2021/03/09 02:41

編集2021/04/13 02:18
pice

総合スコア409

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問