英文:
How to delete a specific item from a list in room db
问题
我想从Room数据库中的列表中删除单个项目,我已经查看了一些文档,其中在@Query
中的SQL查询中使用了 json_remove()
函数,但它不起作用并且抛出错误。
这是我的DAO看起来像这样:
@Entity(tableName = "data_table")
@Serializable
data class DataTable(
val name: String,
@PrimaryKey
val url: String,
val addon: String
)
@Entity(tableName = "entries_table")
@Serializable
@TypeConverters(EntriesTypeConverter::class)
data class EntriesTable(
@PrimaryKey
val name: String,
@TypeConverters(EntriesTypeConverter::class)
val data: List<DataTable>,
)
现在我的问题是,我如何根据DataTable
中的url
从EntriesTable
中的data
中删除特定项目?
我曾使用以下查询语句,我对SQL不熟悉,我查看了来自 sqlite.org
和ChatGPT的示例,但我提取的只是以下查询,它不起作用:
@Query("UPDATE entries_table SET links = json_remove(data, (SELECT json_extract(data, '$[webURL = :url]') FROM entries_table WHERE name = :name)) WHERE name = :name")
我该如何解决这个问题,谢谢:)
英文:
i want to delete a single item from a list in room db, i've gone through few docs where it used json_remove()
function within the SQL query in the @Query
but it doesn't work and throws error;
this is how dao looks:
@Entity(tableName = "data_table")
@Serializable
data class DataTable(
val name: String,
@PrimaryKey
val url: String,
val addon: String
)
@Entity(tableName = "entries_table")
@Serializable
@TypeConverters(EntriesTypeConverter::class)
data class EntriesTable(
@PrimaryKey
val name: String,
@TypeConverters(EntriesTypeConverter::class)
val data: List<DataTable>,
)
now my question is that how can i remove a specific item from the data
in EntriesTable
based on url
which is in theDataTable
i was using this query statement, i'm new to sql things, i've gone thorough example from sqlite.org
and from chatgpt, all i pulled was the following query, which doesnt work:
@Query("UPDATE entries_table SET links = json_remove(data, (SELECT json_extract(data, '$[webURL = :url]') FROM entries_table WHERE name = :name)) WHERE name = :name")
how can i solve this, thankyou:)
答案1
得分: 0
以下是您提供的文本的中文翻译:
如果您使用类型转换器来存储一个列表,实际存储的数据是列表的表示形式作为单个值。SQLite(因此也是Room)不知道个别列表项,而是存储数据的列只是一个单一的包罗万象的值。
通常这样的类型转换器将是数据的JSON字符串表示,其中包含分隔符和名称,允许重建列表对象。
数据表的名称永远不会等于列,而只会成为列的一部分,所以您必须使用SQLite的instr
或LIKE
与掩码来定位SQL的WHERE子句中的行或行。
- 例如
UPDATE .... WHERE instr(data,:name)
或UPDATE .... WHERE data LIKE('&#39;*&#39;||:name||'&#39;*)
。 .... SET links
会有问题,因为根据您的EntriesTable类,没有名为links
的字段,因此也没有名为links
的列。- 无论是
instr
还是LIKE
与掩码都将非常消耗资源,列表越长,消耗的资源就越多。
备选方法
我建议,考虑到您有两个表(即两个带有@Entity注释的类),您可以利用数据库的关系特性,这将更容易地实现对EntriesTable的DataTables的操作,即数据字段/列。
通过这种方法,与其在两个表中存储相同的数据(如果DataTables实际上是一个表的话),您可以有一个用于条目的表和一个用于数据的表,后者包括从data_table行到其父entries_table行的链接(引用/映射)。
在最简单的形式中,entries_table可以是:-
@Entity(tableName = "entries_table")
data class EntriesTable(
@PrimaryKey
val name: String
)
- 即列表将是相关的data_table行
在最简单的形式中,data_table可以是:-
@Entity
data class DataTable(
val name: String,
@PrimaryKey
val url: String,
val addon: String,
@ColumnInfo(index = true)
val entries_reference: String
)
- 注意添加的字段
entries_reference
,为了访问效率而进行索引
然而,由于在data_table中没有引用/链接到父级的data_table行可能会浪费,所以您可能希望包括有效链接(通常称为引用完整性)的强制执行。因此,您可能希望包括外键约束,基本上表示data_table中的行必须具有父行。因此,DataTable类可以是:-
@Entity(
tableName = "data_table",
foreignKeys = [
ForeignKey(
entity = EntriesTable::class,
parentColumns = ["name"],
childColumns = ["entries_reference"],
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
)
]
)
data class DataTable(
val name: String,
@PrimaryKey
val url: String,
val addon: String,
@ColumnInfo(index = true)
val entries_reference: String
)
onDelete
将在删除父级时自动删除子级。onUpdate
将在更改父级的name
时自动更新entries_reference
的值。- 这两个选项可能是可用ON ACTION选项中最有用的两个。
使用这种结构,删除data_table行实际上会更新列表(稍后将进行演示)。这是您希望根据您的问题执行的任务。
您很可能希望检索带有相关data_tables列表的entries_table行,为此您可以使用如下的POJO:-
/* 用于获取带有相关子级(data_table行)的条目的POJO */
data class EntryWithData(
@Embedded
val entriesTable: EntriesTable,
@Relation(
entity = DataTable::class,
parentColumn = "name",
entityColumn = "entries_reference"
)
val dataTableList: List<DataTable>
)
以下是包括2个可用于完成问题任务(即从data_tables列表中删除data_table)的DAO函数:-
@Dao
interface AllDAOs {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(dataTable: DataTable): Long
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(entriesTable: EntriesTable): Long
/* 便利删除注意,room使用主键值(url)来确定要删除的内容(请参见工作演示) */
@Delete
fun delete(entriesTable: EntriesTable): Int
/* 根据提供的url删除 注意,删除条目是不相关的,因为EntriesTable将具有0个或更多个DataTable行的列表 */
@Delete
fun delete(dataTable: DataTable): Int
@Query("DELETE FROM data_table WHERE url=:url")
fun deleteByUrl(url: String)
/* 获取所有数据表的查询 */
@Query("SELECT * FROM data_table")
fun getAllDataTables(): List<DataTable>
/* 获取所有具有相关DataTable列表(每个EntriesTable对象及其DataTable列表)的EntriesWithData对象的查询 */
@Transaction
@Query("SELECT * FROM entries_table")
fun getAllEntriesWithRelatedDataTableList(): List<EntryWithData>
}
演示
为了演示建议的结构,这里有一个简单的@Database
注释的类(请注意,为了简洁起见,将使用主线程):-
@Database(entities = [DataTable::class,EntriesTable::class], exportSchema = false, version = 1)
abstract class TheDatabase: RoomDatabase() {
abstract fun getAllDAOs(): AllDAOs
companion object {
private var instance: TheDatabase?=null
fun getInstance(context: Context): TheDatabase {
if (instance==null) {
instance=Room.databaseBuilder(context,TheDatabase::class.java,"the_database.db")
.allowMainThreadQueries()
.build()
}
return instance as TheDatabase
}
}
英文:
If you use a Type Converter to store a list that actual data stored is a representation of the list as a single value. SQLite (and therefore Room) does not know about individual list items rather the column where the data is stored is just a single all encompassing value.
Typically such a Type Converter will be a JSON String representation of the data with separators and names allowing the list object to be reconstructed.
A DataTables name will never equate to a column, rather it would be part of a column so you would have to use either the SQLite instr
or a LIKE
with a mask to locate the row or rows for the WHERE clause of the SQL.
- e.g.
UPDATE .... WHERE instr(data,:name)
orUPDATE .... WHERE data LIKE('*'||:name||'*')
. .... SET links
is going to be problematic as there is, according to your EntriesTable class, there is no field and thus column namedlinks
.- both
instr
and theLIKE
with a mask will be quite resource expensive, and more so the longer the list.
Alternative Approach
I would suggest, seeing that you have two tables (i.e. two @Entity annotated classes) that you instead utilise the relational aspect of the database which would easily facilitate manipulation of DataTables per EntriesTable i.e. the data field/column.
With this approach instead of storing the same data in both tables (if DataTable is in fact a table) you have a table for the entries and a table for the datas, the latter including a link (reference/map) from a data_table row to it's parent entries_table row.
In the simplest form, the entries_table could be:-
@Entity(tableName = "entries_table")
data class EntriesTable(
@PrimaryKey
val name: String
)
- i.e. the list will be the related data_table rows
In the simplest form data_table could be:-
@Entity
data class DataTable(
val name: String,
@PrimaryKey
val url: String,
val addon: String,
@ColumnInfo(index = true)
val entries_reference: String
)
- note the added field
entries_reference
, which for access efficiency is indexed
However, as a row in the data_table that does not have a reference/link to a parent would likely be a waste it might be an idea to include enforcement of valid links (what is often termed as referential integrity). Thus you may well wish to include Foreign Key constraints that basically say that a row in the data_table MUST have a parent. As such the DataTable class could be:-
@Entity(
tableName = "data_table",
foreignKeys = [
ForeignKey(
entity = EntriesTable::class,
parentColumns = ["name"],
childColumns = ["entries_reference"],
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
)
]
)
data class DataTable(
val name: String,
@PrimaryKey
val url: String,
val addon: String,
@ColumnInfo(index = true)
val entries_reference: String
)
- The
onDelete
will delete the children of the parent automatically if the parent is deleted. - The
onUpdate
will automatically update the value of theentries_reference
if thename
of the parent is changed. - these two are perhaps the most useful of the available ON ACTION options.
With this structure the deletion of a data_table row effectively updates the list (as will be demonstrated later). This being the task that you wish to accomplish according to your question.
You would very likely want to retrieve entries_table rows along with the respective list of data_tables, for this you could have a POJO such as:-
/* POJO for getting Entries with the related children (data_table rows) */
data class EntryWithData(
@Embedded
val entriesTable: EntriesTable,
@Relation(
entity = DataTable::class,
parentColumn = "name",
entityColumn = "entries_reference"
)
val dataTableList: List<DataTable>
)
The following are DAO functions, including 2 delete functions that could be used to accomplish what the task if the question (aka remove a data_table from a list of data_tables):-
@Dao
interface AllDAOs {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(dataTable: DataTable): Long
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(entriesTable: EntriesTable): Long
/* Convenience Deletes NOTE room uses the Primary Key value (url) to determine what to delete (see working demo) */
@Delete
fun delete(entriesTable: EntriesTable): Int
/* Delete according to provided url */
/* NOTE UPDATE to remove is irrelevant as an EntriesTable will have a List of 0 or more DataTable rows */
@Delete
fun delete(dataTable: DataTable): Int
@Query("DELETE FROM data_table WHERE url=:url")
fun deleteByUrl(url: String)
/* Query to get all DataTables */
@Query("SELECT * FROM data_table")
fun getAllDataTables(): List<DataTable>
/* Query to get all EntryWithData objects (Each EntriesTable Object with it's list of DataTables) */
@Transaction
@Query("SELECT * FROM entries_table")
fun getAllEntriesWithRelatedDataTableList(): List<EntryWithData>
}
Demonstration
To facilitate demonstration of the suggested structure a simple @Database
annotated class (noting that for brevity the main thread will be used):-
@Database(entities = [DataTable::class,EntriesTable::class], exportSchema = false, version = 1)
abstract class TheDatabase: RoomDatabase() {
abstract fun getAllDAOs(): AllDAOs
companion object {
private var instance: TheDatabase?=null
fun getInstance(context: Context): TheDatabase {
if (instance==null) {
instance=Room.databaseBuilder(context,TheDatabase::class.java,"the_database.db")
.allowMainThreadQueries()
.build()
}
return instance as TheDatabase
}
}
}
To actually demonstrate then the following activity code:-
class MainActivity : AppCompatActivity() {
lateinit var db: TheDatabase
lateinit var dao: AllDAOs
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
db = TheDatabase.getInstance(this)
dao = db.getAllDAOs()
/* use the database to add some data, and then delete data, writing the contents of the tables at various stages */
doSomeDbWork()
}
fun doSomeDbWork() {
/* Add 4 EntriesTable rows */
dao.insert(EntriesTable("Entry1"))
dao.insert(EntriesTable("Entry2"))
dao.insert(EntriesTable("Entry3"))
dao.insert(EntriesTable("Entry4"))
/* Add related DataTable rows, 3 related to Entry1, 3 related to Entry 2, 1 related to Entry3 and 1 related to Entry 4 */
dao.insert(DataTable("DT01","dt01url","addonA","Entry1"))
dao.insert(DataTable("DT02","dt02url","addonB","Entry1"))
dao.insert(DataTable("DT03","dt03url","addonC","Entry1"))
dao.insert(DataTable("DT04","dt04url","addonA","Entry2"))
dao.insert(DataTable("DT05","dt05url" ,"addon1","Entry2"))
dao.insert(DataTable("DT06","dt06url","addonD","Entry2"))
dao.insert(DataTable("DT07","dt07url","addonE","Entry3"))
dao.insert(DataTable("DT08","dt08url","addonF","Entry4"))
/* Show the initial data */
logEntriesWithDataTableLists("STAGE1")
/* example of deleting non existent datatable */
dao.delete(DataTable("does not matter","not a saved url","again does not matter","again does not matter"))
logEntriesWithDataTableLists("STAGE2")
/* example of deleting existing datatable shows that all that matters is the Primary key value as that is what Room uses to ascertain what to delete */
dao.delete(DataTable("does not matter","dt07url","again does not matter","again does not matter"))
logEntriesWithDataTableLists("STAGE3")
/* example of alternative deleting by url */
dao.deleteByUrl("dt01url")
logEntriesWithDataTableLists("STAGE4")
/* example of deleting an Entry and the deletion being cascaded to the children of the entry */
dao.delete(EntriesTable("Entry3"))
logEntriesWithDataTableLists("STAGE5")
}
fun logEntriesWithDataTableLists(tagSuffix: String){
for(ewd in dao.getAllEntriesWithRelatedDataTableList()) {
val sb=StringBuilder()
for (dt in ewd.dataTableList) {
sb.append("\n\tDT name = ${dt.name} URL=${dt.url} ADDON=${dt.addon} REFERS TO PARENT ENTRY ${dt.entries_reference}")
}
Log.d("DBINFO_${tagSuffix}","Entry is ${ewd.entriesTable.name} it has ${ewd.dataTableList.size} related DT's. They are:-${sb}")
}
logAllDatatables(tagSuffix)
}
fun logAllDatatables(tagSuffix: String) {
for (dt in dao.getAllDataTables()) {
Log.d("DBINFO_DT_$tagSuffix","DT is ${dt.name} URL is ${dt.url} ADDON is ${dt.addon} REFERS TO PARENT ENTRY ${dt.entries_reference}")
}
}
}
Result
When run (for the first time) then the log includes:-
STAGE1
This is after the 4 rows have been added to the entries_table and the 8 rows have been added to the data_table table.
Of the 8 data_table rows 3 are related to Entry1, 2 are related to Entry2 and 1 each are related to Entry 3 and Entry 4.
2023-06-19 14:51:48.532 D/DBINFO_STAGE1: Entry is Entry1 it has 3 related DT's. They are:-
DT name = DT01 URL=dt01url ADDON=addonA REFERS TO PARENT ENTRY Entry1
DT name = DT02 URL=dt02url ADDON=addonB REFERS TO PARENT ENTRY Entry1
DT name = DT03 URL=dt03url ADDON=addonC REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.532 D/DBINFO_STAGE1: Entry is Entry2 it has 3 related DT's. They are:-
DT name = DT04 URL=dt04url ADDON=addonA REFERS TO PARENT ENTRY Entry2
DT name = DT05 URL=dt05url ADDON=addon1 REFERS TO PARENT ENTRY Entry2
DT name = DT06 URL=dt06url ADDON=addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.532 D/DBINFO_STAGE1: Entry is Entry3 it has 1 related DT's. They are:-
DT name = DT07 URL=dt07url ADDON=addonE REFERS TO PARENT ENTRY Entry3
2023-06-19 14:51:48.532 D/DBINFO_STAGE1: Entry is Entry4 it has 1 related DT's. They are:-
DT name = DT08 URL=dt08url ADDON=addonF REFERS TO PARENT ENTRY Entry4
2023-06-19 14:51:48.534 D/DBINFO_DT_STAGE1: DT is DT01 URL is dt01url ADDON is addonA REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.534 D/DBINFO_DT_STAGE1: DT is DT02 URL is dt02url ADDON is addonB REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.534 D/DBINFO_DT_STAGE1: DT is DT03 URL is dt03url ADDON is addonC REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.534 D/DBINFO_DT_STAGE1: DT is DT04 URL is dt04url ADDON is addonA REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.534 D/DBINFO_DT_STAGE1: DT is DT05 URL is dt05url ADDON is addon1 REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.534 D/DBINFO_DT_STAGE1: DT is DT06 URL is dt06url ADDON is addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.535 D/DBINFO_DT_STAGE1: DT is DT07 URL is dt07url ADDON is addonE REFERS TO PARENT ENTRY Entry3
2023-06-19 14:51:48.535 D/DBINFO_DT_STAGE1: DT is DT08 URL is dt08url ADDON is addonF REFERS TO PARENT ENTRY Entry4
STAGE2
This is after the attempt to delete row(s) that do not exist in the data_table i.e. the specified url is not a row.
- note as will be shown later, the other values of the passed DataTable class are irrelevant.
As such the resultant data is identical to STAGE1.
:-
2023-06-19 14:51:48.542 D/DBINFO_STAGE2: Entry is Entry1 it has 3 related DT's. They are:-
DT name = DT01 URL=dt01url ADDON=addonA REFERS TO PARENT ENTRY Entry1
DT name = DT02 URL=dt02url ADDON=addonB REFERS TO PARENT ENTRY Entry1
DT name = DT03 URL=dt03url ADDON=addonC REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.543 D/DBINFO_STAGE2: Entry is Entry2 it has 3 related DT's. They are:-
DT name = DT04 URL=dt04url ADDON=addonA REFERS TO PARENT ENTRY Entry2
DT name = DT05 URL=dt05url ADDON=addon1 REFERS TO PARENT ENTRY Entry2
DT name = DT06 URL=dt06url ADDON=addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.543 D/DBINFO_STAGE2: Entry is Entry3 it has 1 related DT's. They are:-
DT name = DT07 URL=dt07url ADDON=addonE REFERS TO PARENT ENTRY Entry3
2023-06-19 14:51:48.543 D/DBINFO_STAGE2: Entry is Entry4 it has 1 related DT's. They are:-
DT name = DT08 URL=dt08url ADDON=addonF REFERS TO PARENT ENTRY Entry4
2023-06-19 14:51:48.546 D/DBINFO_DT_STAGE2: DT is DT01 URL is dt01url ADDON is addonA REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.546 D/DBINFO_DT_STAGE2: DT is DT02 URL is dt02url ADDON is addonB REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.546 D/DBINFO_DT_STAGE2: DT is DT03 URL is dt03url ADDON is addonC REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.547 D/DBINFO_DT_STAGE2: DT is DT04 URL is dt04url ADDON is addonA REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.547 D/DBINFO_DT_STAGE2: DT is DT05 URL is dt05url ADDON is addon1 REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.547 D/DBINFO_DT_STAGE2: DT is DT06 URL is dt06url ADDON is addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.547 D/DBINFO_DT_STAGE2: DT is DT07 URL is dt07url ADDON is addonE REFERS TO PARENT ENTRY Entry3
2023-06-19 14:51:48.547 D/DBINFO_DT_STAGE2: DT is DT08 URL is dt08url ADDON is addonF REFERS TO PARENT ENTRY Entry4
STAGE3
Stage 3 is the deletion of a known/existing data_table row, i.e. the row that has the url value of dt07url
(the 7th inserted data_table row which has Entry3 as it's parent).
-
Note how Entry3 now has 0 related data_tables in it's list as per
D/DBINFO_STAGE3: Entry is Entry3 it has 0 related DT's.
i.e. the list, which was just the 1 item, has been updated to be 0 items. -
also note that even though the other fields of the passed DataTable are obviously incorrect, that the row is in fact deleted. i.e. confirming/demonstrating that it is just the PrimaryKey that is used to ascertain the row to be deleted.
:-
2023-06-19 14:51:48.554 D/DBINFO_STAGE3: Entry is Entry1 it has 3 related DT's. They are:-
DT name = DT01 URL=dt01url ADDON=addonA REFERS TO PARENT ENTRY Entry1
DT name = DT02 URL=dt02url ADDON=addonB REFERS TO PARENT ENTRY Entry1
DT name = DT03 URL=dt03url ADDON=addonC REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.555 D/DBINFO_STAGE3: Entry is Entry2 it has 3 related DT's. They are:-
DT name = DT04 URL=dt04url ADDON=addonA REFERS TO PARENT ENTRY Entry2
DT name = DT05 URL=dt05url ADDON=addon1 REFERS TO PARENT ENTRY Entry2
DT name = DT06 URL=dt06url ADDON=addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.555 D/DBINFO_STAGE3: Entry is Entry3 it has 0 related DT's. They are:-
2023-06-19 14:51:48.555 D/DBINFO_STAGE3: Entry is Entry4 it has 1 related DT's. They are:-
DT name = DT08 URL=dt08url ADDON=addonF REFERS TO PARENT ENTRY Entry4
2023-06-19 14:51:48.561 D/DBINFO_DT_STAGE3: DT is DT01 URL is dt01url ADDON is addonA REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.562 D/DBINFO_DT_STAGE3: DT is DT02 URL is dt02url ADDON is addonB REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.562 D/DBINFO_DT_STAGE3: DT is DT03 URL is dt03url ADDON is addonC REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.562 D/DBINFO_DT_STAGE3: DT is DT04 URL is dt04url ADDON is addonA REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.562 D/DBINFO_DT_STAGE3: DT is DT05 URL is dt05url ADDON is addon1 REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.562 D/DBINFO_DT_STAGE3: DT is DT06 URL is dt06url ADDON is addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.562 D/DBINFO_DT_STAGE3: DT is DT08 URL is dt08url ADDON is addonF REFERS TO PARENT ENTRY Entry4
STAGE4
Stage 4 deletes another row from the data_table, this time using the alternative delete function which is an @Query
that takes just the url as it's parameter as opposed to the convenience @Delete
which expects an object as it's parameter.
- Note that the disadvantage with this approach is that the number of affected (deleted) rows is not returned.
:-
2023-06-19 14:51:48.567 D/DBINFO_STAGE4: Entry is Entry1 it has 2 related DT's. They are:-
DT name = DT02 URL=dt02url ADDON=addonB REFERS TO PARENT ENTRY Entry1
DT name = DT03 URL=dt03url ADDON=addonC REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.567 D/DBINFO_STAGE4: Entry is Entry2 it has 3 related DT's. They are:-
DT name = DT04 URL=dt04url ADDON=addonA REFERS TO PARENT ENTRY Entry2
DT name = DT05 URL=dt05url ADDON=addon1 REFERS TO PARENT ENTRY Entry2
DT name = DT06 URL=dt06url ADDON=addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.567 D/DBINFO_STAGE4: Entry is Entry3 it has 0 related DT's. They are:-
2023-06-19 14:51:48.567 D/DBINFO_STAGE4: Entry is Entry4 it has 1 related DT's. They are:-
DT name = DT08 URL=dt08url ADDON=addonF REFERS TO PARENT ENTRY Entry4
2023-06-19 14:51:48.570 D/DBINFO_DT_STAGE4: DT is DT02 URL is dt02url ADDON is addonB REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.570 D/DBINFO_DT_STAGE4: DT is DT03 URL is dt03url ADDON is addonC REFERS TO PARENT ENTRY Entry1
2023-06-19 14:51:48.570 D/DBINFO_DT_STAGE4: DT is DT04 URL is dt04url ADDON is addonA REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.570 D/DBINFO_DT_STAGE4: DT is DT05 URL is dt05url ADDON is addon1 REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.570 D/DBINFO_DT_STAGE4: DT is DT06 URL is dt06url ADDON is addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.570 D/DBINFO_DT_STAGE4: DT is DT08 URL is dt08url ADDON is addonF REFERS TO PARENT ENTRY Entry4
STAGE5
Stage 5 demonstrates the onDelete
CASCADE
option. The entries_table Entry1 row is deleted and as can be seen so are the 2 remain data_table rows.
2023-06-19 14:51:48.576 D/DBINFO_STAGE5: Entry is Entry2 it has 3 related DT's. They are:-
DT name = DT04 URL=dt04url ADDON=addonA REFERS TO PARENT ENTRY Entry2
DT name = DT05 URL=dt05url ADDON=addon1 REFERS TO PARENT ENTRY Entry2
DT name = DT06 URL=dt06url ADDON=addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.576 D/DBINFO_STAGE5: Entry is Entry3 it has 0 related DT's. They are:-
2023-06-19 14:51:48.576 D/DBINFO_STAGE5: Entry is Entry4 it has 1 related DT's. They are:-
DT name = DT08 URL=dt08url ADDON=addonF REFERS TO PARENT ENTRY Entry4
2023-06-19 14:51:48.580 D/DBINFO_DT_STAGE5: DT is DT04 URL is dt04url ADDON is addonA REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.580 D/DBINFO_DT_STAGE5: DT is DT05 URL is dt05url ADDON is addon1 REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.580 D/DBINFO_DT_STAGE5: DT is DT06 URL is dt06url ADDON is addonD REFERS TO PARENT ENTRY Entry2
2023-06-19 14:51:48.580 D/DBINFO_DT_STAGE5: DT is DT08 URL is dt08url ADDON is addonF REFERS TO PARENT ENTRY Entry4
答案2
得分: -1
查看DAO对象的定义 - https://developer.android.com/training/data-storage/room/accessing-data#anatomy
你可以在你的DAO中编写类似以下的代码:
@Dao
interface DataTableDao {
...
@Delete
fun delete(dataTable: DataTable)
@Query("DELETE FROM datatable WHERE url = :urlInput")
fun conditionalDelete(urlInput: String)
}
你可以使用 delete
函数(https://developer.android.com/training/data-storage/room/accessing-data#convenience-delete)来从DataTable中删除特定条目,或者你可以定义自己的查询(https://developer.android.com/training/data-storage/room/accessing-data#query)来执行更复杂的操作。
英文:
See the definition of DAO object - https://developer.android.com/training/data-storage/room/accessing-data#anatomy
You can write something like the following in your DAO:
@Dao
interface DataTableDao {
...
@Delete
fun delete(dataTable: DataTable)
@Query("DELETE FROM datatable WHERE url = :urlInput")
fun conditionalDelete(urlInput: String)
}
You can use the delete
function (https://developer.android.com/training/data-storage/room/accessing-data#convenience-delete) to remove specific entry from DataTable, or you can define your own query (https://developer.android.com/training/data-storage/room/accessing-data#query) to do something more complex.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论