简体   繁体   中英

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?

For example, I have id as primary key. 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.

All you have to do is create a Query like so:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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