2014-09-06 168 views
0

如何檢查數據庫中是否存在記錄(ContentValues)?如何檢查記錄是否已經存在?

public void insertData(String ActId,String Dstcode,String Mandicode,String Traderlicno,String licenseId,String licensename,String Gatepassno,String FarmerId,String Farmername,String FarmerFatherName,String CommodityGrpCode,String CommodityCode,String VartyCode,String Cwght,String UntCode,String FnlPrice,String lgndate,String Ip,String dte,String IEMIN0,String ModelNo,String BoardName,String BrandName,String SIMNo) 
    { 

    ContentValues cv = new ContentValues(); 

     cv.put(DatabaseHandler.AUCTIONINFO_ActId, ActId); 
     cv.put(DatabaseHandler.AUCTIONINFO_Dstcode, Dstcode); 
     cv.put(DatabaseHandler.AUCTIONINFO_Mandicode, Mandicode); 
     cv.put(DatabaseHandler.AUCTIONINFO_Traderlicno, Traderlicno); 
     cv.put(DatabaseHandler.AUCTIONINFO_licenseId, licenseId); 
     cv.put(DatabaseHandler.AUCTIONINFO_licensename, licensename); 
     cv.put(DatabaseHandler.AUCTIONINFO_Gatepassno, Gatepassno); 
     cv.put(DatabaseHandler.AUCTIONINFO_FarmerId, FarmerId); 
     cv.put(DatabaseHandler.AUCTIONINFO_Farmername, Farmername); 
     cv.put(DatabaseHandler.AUCTIONINFO_FarmerFatherName, FarmerFatherName); 
     cv.put(DatabaseHandler.AUCTIONINFO_CommodityGrpCode, CommodityGrpCode); 
     cv.put(DatabaseHandler.AUCTIONINFO_CommodityCode, CommodityCode); 
     cv.put(DatabaseHandler.AUCTIONINFO_VartyCode, VartyCode); 
     cv.put(DatabaseHandler.AUCTIONINFO_Cwght, Cwght); 
     cv.put(DatabaseHandler.AUCTIONINFO_UntCode, UntCode); 
     cv.put(DatabaseHandler.AUCTIONINFO_FnlPrice, FnlPrice); 
     cv.put(DatabaseHandler.AUCTIONINFO_lgndate, lgndate); 
     cv.put(DatabaseHandler.AUCTIONINFO_Ip, Ip); 
     cv.put(DatabaseHandler.AUCTIONINFO_dte, dte); 
     cv.put(DatabaseHandler.AUCTIONINFO_IEMIN0, IEMIN0); 
     cv.put(DatabaseHandler.AUCTIONINFO_ModelNo, ModelNo); 
     cv.put(DatabaseHandler.AUCTIONINFO_BoardName, BoardName); 
     cv.put(DatabaseHandler.AUCTIONINFO_BrandName, BrandName); 
     cv.put(DatabaseHandler.AUCTIONINFO_SIMNo, SIMNo); 

      database.insert(DatabaseHandler.TABLE_AUCTIONINFO, null, cv); 

}

+0

你有問題嗎? – 2014-09-06 10:08:32

+0

運行一個簡單的查詢 - '從TABLE_AUCTIONINFO中選擇AUCTIONINFO_ActId,其中AUCTIONINFO_licenseId = licenseId;'如果光標爲空意味着記錄不存在 – 2014-09-06 10:09:26

+0

您的記錄標準是什麼?有一些id存在或所有columna匹配?如果一行已經存在,你想要做什麼? – laalto 2014-09-06 11:27:51

回答

0

編輯:

myDB= openOrCreateDatabase("DBName", MODE_PRIVATE, null); 
myDB.execSQL("CREATE TABLE IF NOT EXISTS MyTable (your query goes here);"); 

Cursor cursor = myDB.rawQuery("select AUCTIONINFO_ActId from TABLE_AUCTIONINFO where AUCTIONINFO_licenseId = licenseId;", null); 
if(cursor.getCount()!=0) 
{ 
    // now you can insert the record 
} 
else 
{ 

} 
+0

我想檢查數據庫是否已經存在,比數據庫沒有插入,否則插入數據庫.... – ROHAN 2014-09-06 10:45:08

+0

我編輯了我的答案 – 2014-09-06 10:51:33

相關問題