Home > OS >  Validating data from a database
Validating data from a database

Time:07-02

I am trying to create a registration screen. When the registration button is pressed, I want the program to check if the provided username already exists in the database. Is it does, it will not proceed with the registration. Problem is, I cannot, find a way to access the data. Here's the related code

@Database(entities = arrayOf(User::class), version = 1)
abstract class UsersDatabase : RoomDatabase() {

    abstract fun usersDao(): UsersDao

    companion object {
        @Volatile
        private var INSTANCE: UsersDatabase? = null

        fun getDatabase(context: Context): UsersDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    UsersDatabase::class.java,
                    "users_database"
                ).fallbackToDestructiveMigration().build()
                INSTANCE = instance
                instance
            }
        }
      }
    }
@Dao
interface UsersDao {
        
   @Insert(onConflict = OnConflictStrategy.ABORT)
   fun insertUser(user: User)
        
    @Query("Select * from users_table Order By id desc")
    fun readAllData(): LiveData<List<User>>
        
    @Query("Select * from users_table Where username = :username")
    fun readUsernnames(username: User): User?
        
    @Query("Select username from users_table Where username = :username")
    fun readUsernnamesUsername(username: String): String?
        
    @Query("Select * from users_table Where email = :email")
    fun readEmails(email: String): User?
        
    @Query("Select * from users_table Where password = :password")
    fun readPasswords(password: String): User?}
}

class UsersRepository(private val usersDao: UsersDao) {
   val readAllData = usersDao
   val readSomeData = listOf(usersDao.toString())
        
   val readData = usersDao.readAllData()
        
   fun addUser(user: User){
      usersDao.insertUser(user)
   }
        
   fun userExists(user: User): User? {
      return usersDao.readUsernnames(user)
   }
}

CodePudding user response:

To access the data you will need a ViewModel.

which might look like this:

class MyViewModel(application: Application) : AndroidViewModel(application) {

   val data: LiveData<List<User>>
   private val repository: UsersRepository

   init {
      val dao = UsersDatabase.getDatabase(application).usersDao()
      val repository = UsersRepository(dao)
      data = repository.readData
   }

   fun addUser(user: User) = repository.addUser(user)


   fun userExists(user: User): User? = repository.userExists(user)
}

and with that, in your activity's onCreate:

override fun onCreate(savedInstanceState: Bundle?) {
        ...
        val viewModel: MyViewModel = ViewModelProvider(this).get(MyViewModel::class.java)
        /* and below that you might call your data, like so:
           viewModel.data
           viewModel.addUser(user) etc.
        */
        ...
} 

Just as a note:

I noticed that you are not using coroutines, which will lead to longer loadings and might even lead to ANRs because the application will be busy reading data from your database (if it contains multiple entities), so please read about using suspend functions with room ( or using flows / liveData ).

Also, this code will not be possible to test, at least not easily, if you want to improve the testability of your code take a look at this

  • Related