2016-07-20 204 views
1

我想對一個帳戶系統進行編碼,帳戶存儲在SQLite數據庫中。每當有人想要創建一個帳戶,我首先要檢查是否已經有一個帳戶使用相同的電子郵件(該電子郵件不是主鍵,而是簡單的文本)。檢查SQLite數據庫中是否已經存在一個值

public boolean checkemail(String email) 
{ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    String Query = "Select * from " + TABLE_KANIDAT + " where " + KEY_KEMAIL + " = " + "'"+email+"'"; 
    Cursor cursor = db.rawQuery(Query, null); 
    if(cursor.getCount() <= 0) 
    { 
     cursor.close(); 
     return false; 
    } 
    else 
    { 
     cursor.close(); 
     return true; 
    } 
} 
+0

'選擇存在(SELECT * FROM TABLE_KANIDAT其中KEY_KEMAIL = 'theemail' 極限1);'返回1,如果一個行存在,否則爲0你需要也請您陳述你的陳述。 –

+0

這個代碼有什麼問題? –

回答

1

創建一個方法

public boolean checkAlreadyExist(String email) 
    { 
     String query = SELECT + YOUR_EMAIL_COLUMN + FROM + TABLE_NAME + WHERE + YOUR_EMAIL_COLUMN + " =?"; 
     Cursor cursor = db.rawQuery(query, new String[]{email}); 
     if (cursor.getCount() > 0) 
     { 
      return false; 
     } 
     else 
      return true; 
    } 
0
Try this 

     public boolean rowIdExists(String StrId) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery("select id from " + TABLE_USERRATE 
      + " where id=?", new String[]{StrId}); 
    boolean exists = (cursor.getCount() > 0); 
    /*cursor.close(); 
    db.close();*/ 
    return exists; 
} 


    if (rowIdExists("ValuesId")) { 
      //do something 
      } else { 
       //do something 
      } 
相關問題