2014-10-27 71 views
-1

我想運行這個查詢,但它只是不工作,我似乎無法找到它有什麼問題。運行原始的sqlite查詢

public boolean verifyUser(String name , String pword){ 
    boolean result = false; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    try { 
    Cursor res = db.rawQuery("select * from user where username="+name+" and password="+pword+"", null); 

     int count = res.getCount(); 

     res.moveToFirst(); 
     if (count == 0) 
     { 
      result = false; 
     } 
     else{ 
     result = true; 
     } 
    } 
     catch (SQLiteException se) { 
      Log.e(getClass().getSimpleName(), "Could not log in"); 
     } 
     return result; 
     //return true; 
    } 

回答

3

在SQL中,您需要將字符串文字放在'single quotes'中。更重要的是,使用參數:

Cursor res = db.rawQuery("select * from user where username=? and password=?", 
    new String[] { name, pword }); 
0

複製並粘貼以下查詢

select * from user where username='"+name+"' and password='"+pword+"'" 

其實你缺少單引號

+0

當值包含一個引號時,這將爆炸。 – 2014-10-27 12:59:57

+0

試過這個,但不知何故我的程序仍然崩潰。有沒有辦法通過命令提示符查看數據庫中發生了什麼?我正在使用eclipse – thesma 2014-10-27 13:01:35

+0

@CL請澄清請 – nobalG 2014-10-27 13:05:36

0

經過這一點,它的編輯你的代碼。

public boolean verifyUser(String name , String pword){ 
    boolean result = false; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    try { 
    Cursor res = db.rawQuery("select * from user where username='"+name+"' and password='"+pword+"'", null); 

     int count = res.getCount(); 

     //res.moveToFirst(); //Remove this line (It may be crashing because of this line.) 
     if (count == 0) 
     { 
      result = false; 
     } 
     else{ 
     res.moveToFirst(); 
     result = true; 
     } 
    } 
     catch (SQLiteException se) { 
      Log.e(getClass().getSimpleName(), "Could not log in"); 
     } 
     return result; 
     //return true; 
    } 
+0

現貨差異不是一個有用的答案。 – 2014-10-27 13:00:34

+0

@CL ....如果您知道答案,請回復此問題。我看到你總是投入某人的回答,但從未給出解決方案。 – samsad 2014-10-27 13:06:06

+0

@samsad請耐心等待,我們的方法可能有問題,(如果不是這樣,請讓CL解釋) – nobalG 2014-10-27 13:08:09