如何检查值已经excite或不在android中的sqlite数据库?

问题描述:

在我的应用程序中,我在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值。任何人都知道,请帮我解决这个问题。

以下是帮助您查找该值是否可用于数据库的功能。

下面请我的查询替换查询..

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

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