How To Find Out User Is Present In Sqlite Database Using Android?
Solution 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:
funuserPresent(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.
Solution 2:
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.
Solution 3:
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)
Post a Comment for "How To Find Out User Is Present In Sqlite Database Using Android?"