Android studio 連線sqlist資料庫,賬號密碼錯誤仍能登入的原因

辞楠發表於2024-06-10

昨天在寫Android studio的大作業,寫到連線sqlist資料庫實現登入的時候明明賬號密碼都不正確,但是使用者卻可以登入,我原先以為是我sql語句寫錯了,將sql語句從

Cursor cursor=db.rawQuery("select * from user where name like ? and password like ?",new String[]{name,password});
改成
Cursor cursor = db.query("user",new String[]{"name,password"},"name=? and password=?",new String[]{name,password},null,null,null);
但是依舊不成功,然後我看了看我的判斷語句才知道,是判斷cursor是否為空哪裡寫錯了,正確的程式碼是
if(cursor.moveToFirst())
{
flag=true;
cursor.close();
}
System.out.println(flag);
return flag;
但是我寫成了
if(cursor!=null)
{
flag=true;
cursor.close();
}
System.out.println(flag);
return flag;
rursor的輸出是android.database.sqlite.SQLiteCursor@da50f0f,它當然不是空了,就因為這個小小的錯誤搞了半天才弄好。

相關文章