How to find out user is present in Sqlite Database using android?

好久不见. 提交于 2019-12-11 08:17:14

问题


I am working on a login/register app just for training as I am a beginner everything is working except for a small problem but I don't know how to fix it , I tried searching but couldn't find anything here is the part of my database.kt

   fun userPresent (user: String,pass: String):Boolean {
       val db = writableDatabase
       val query = "select * from $TABLE_NAME where username = $user and password = $pass"
       val cursor = db.rawQuery(query,null)

       if (cursor.count <= 0) {
           cursor.close()
           return false }
       cursor.close()
       return true }

mainactivity.kt

if (database.userPresent(user = username,pass = password)) {
                intent.putExtra("text", "Welcome , $username $password")
                startActivity(intent)
                Toast.makeText(this,"Logged In Successfully",Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this,"Wrong Username/Password",Toast.LENGTH_SHORT).show()
            }

My logCat Error

android.database.sqlite.SQLiteException: no such column: ss (code 1 SQLITE_ERROR): , while compiling: select * from user_table where username = ss and password = ss
at com.example.myapplication.DatabaseHelper.userPresent(DatabaseHelper.kt:52)
at com.example.myapplication.MainActivity$onCreate$1.onClick(MainActivity.kt:49)

also I am not sure on why you have to close the cursor. thank you in advance


回答1:


The problem with your code is not that you pass the arguments as Integer (you don't), but that the arguments are not recognized as TEXT literals because they are not enclosed in single quotes, so SQLite thinks they are column names.

The recommended way of passing the parameters to rawQuery() is this:

fun userPresent (user: String, pass: String): Boolean {
    val db = writableDatabase 
    val query = "select * from $TABLE_NAME where username = ? and password = ?"
    val cursor = db.rawQuery(query, arrayOf(user, pass))
    val result = cursor.count > 0
    cursor.close()
    db.close()
    return result
}

The placeholders ? will take their values from the corresponding items of the array passed as the 2nd argument of rawQuery() and you don't need to concatenate the single quotes so you avoid the risk of sql injection.
After that and before the return statement you must close both the Cursor and the db object.




回答2:


You problem is that your user and pass variables are String and you must put your String variables

into " " or ' '

But You put them as Integer while your table columns (user and pass) are not Integer and They are String

Use String arguments and put your user and pass variables in it. It will help you you to solve your problem.

val query="select * from $TABLE_NAME where username =? and password = ?"
val arguments= arrayOf(user, pass)
db.rawQuery(query, arguments)

Of course as the Second way you can try this too

val query="select * from $TABLE_NAME where username = \'$user\' and password = \'$pass\'"
db.rawQuery(query, null)



回答3:


The final query should be like below

select * from user_table where username = 'ss' and password = 'ss';

Wrap your username and password with single quotes.



来源:https://stackoverflow.com/questions/58152742/how-to-find-out-user-is-present-in-sqlite-database-using-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!