简体   繁体   English

Android,如何检查 Room DB 中是否已存在列?

[英]Android, how to check if column already exists in Room DB?

How can I check if a specific column entry already exists in Room database?如何检查 Room 数据库中是否已存在特定列条目?

For example, I have id as primary key.例如,我将 id 作为主键。 But also there is a column "username" which is also unique.但还有一列“用户名”也是唯一的。 So I want to check and see if a specific username like "Marshal" already exists in database to warn user about it.所以我想检查一下数据库中是否已经存在像“Marshal”这样的特定用户名来警告用户。

All you have to do is create a Query like so:你所要做的就是像这样创建一个Query

@Query("SELECT COUNT(username) FROM MY_ENTITY WHERE username = :username")
fun getUsername(username: String) : LiveData<Integer>

If the result is 0 then there is no user with the same name if not you'll get the number of people with the same username.如果结果为 0,则没有同名的用户,否则您将获得具有相同用户名的人数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM