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}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。