2012-07-03 23 views
0

我正在開發一個android應用程序,其中我有一個本地database.I要,如果用戶已經進入藥品名稱a.Then再次我去編輯文本和鍵入一。它不應該在本地數據庫中添加,如何做到這一點錯誤獲取本地數據庫在Android應用程序的檢查

我想請點擊此鏈接

Insert if not exists?

任何人都可以指導我

感謝 圖莎爾

+0

給出一些你試過的代碼示例 – sunil

+0

不知道你想要什麼幫助。插入行如果不存在就是你想要的。看看鏈接的例子,它看起來是正確的。 –

+0

你爲什麼鏈接沒有工作?看起來像一個很好的解決方案 – RvdK

回答

1

試試這個方法...它可以幫助你。

String sql = "select * from " 
       + TABLE_NAME + " WHERE placeId = '" + placeId + "'"; 
     Cursor cursorinfo = null; 
     try { 
      database = databaseHelper.getReadableDatabase(); 
      cursorinfo = database.rawQuery(sql, null); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

if (cursorinfo.moveToFirst()) { 
      try { 
       ///Already Exist data.. 
      } finally { 
       database.close(); 
       cursorinfo.close(); 
      } 

     } else { 
      database = databaseHelper.getWritableDatabase(); 
      try { 
       database.insert(TABLE_NAME, "", values); 
      } finally { 
       database.close(); 
       cursorinfo.close(); 
      } 
     } 

謝謝..

+0

@abdull ,,,,,,我需要在我的數據庫類中添加此代碼? – Harpreet

+0

這是您的數據庫類...發佈... –

1

你只需要檢查,如果你的Record已經存在於表或沒有,你遵循了正確的答案,我不知道爲什麼你是不是能夠做到這一點,這就是你必須要做的。

Cursor cursor = database.query(tableName, null, "mediName="+editTextValue, null, null, null, null); 
boolean exists = (cursor.getCount() > 0); 
cursor.close(); 

if(!exists) 
{ 
    database.insert(DATABASE_TABLE, null, updateValues); 
} 
else 
{ 
    // Record Already there. 
} 
相關問題