2013-04-25 53 views
0

在我的應用程序中,我在SQLite數據庫中保存了賬單號碼。在添加新賬單號碼之前,如何檢查賬單號碼是否存在於數據庫中。如何檢查值已經excite或不在android中的sqlite數據庫?

我主類的代碼是,

String bill_no_excist_or_not = db.billno_exist_or_not(""+et_bill_number.getText().toString()); 
Log.v("Bill No", ""+bill_no_excist_or_not); 

我的DB代碼,

String billno_exist_or_not(String bill_number){ 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.query(TABLE_BILL_DETAILS, new String[] { KEY_BILL_NUMBER }, KEY_BILL_NUMBER + "=?" 
        + new String[] { bill_number }, null, null, null, null); 

//after this i don't know how to return the values 

    return bill_number; 
} 

我不知道如何檢查這已經是可用的或者未在DB值。任何人都知道,請幫我解決這個問題。

回答

1

以下是幫助您查找該值是否可用於數據庫的功能。

下面請我的查詢替換查詢..

public int isUserAvailable(int userId) 
    { 
     int number = 0; 
     Cursor c = null; 
     try 
     { 
      c = db.rawQuery("select user_id from user_table where user_id = ?", new String[] {String.valueOf(userId)}); 

      if(c.getCount() != 0) 
       number = c.getCount(); 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if(c!=null) c.close(); 
     } 
     return number; 
    } 
+0

這裏我不是清楚的瞭解,請解釋一下我在這個'if(c.getCount()!= 0) number = c.getCount(); '代碼。 – Yugesh 2013-04-25 06:53:42

+0

如果您的查詢成功運行並找到匹配結果,則c.getCount返回匹配結果的數量...因此,我們可以得出結論,在數據庫中有一些記錄.. – 2013-04-25 07:10:47

+0

好的謝謝Chirag Raval。 – Yugesh 2013-04-25 07:18:27

1

讓您的表UNIQUEKEY_BILL_NUMBER列,你可以直接插入使用insertWithOnConflictSQLiteDatabase.CONFLICT_IGNORE

相關問題