0

我想檢查SQLite中的數據,如果已經存在可以更新或插入。我正在檢查這樣的代碼,我在下面提到。如何檢查SQLite中的數據如果已經存在更新或在Android中插入

代碼:

public long addmenus(String navigationdrawer,String optionname) 
    { 
     SQLiteDatabase menus=this.getWritableDatabase(); 
     try { 

     ContentValues values=new ContentValues(); 
     values.put(HEADER_NAME,navigationdrawer); 
     values.put(CHILD_NAME,optionname); 

     // menus.insert(TABLE_NAME,null,values); 
      // String owner=optionname; 

      Cursor cursor = menus.rawQuery("select * from TABLE_NAME where CHILD_NAME ='"+ optionname +"'", null); 

     if(cursor.getCount()<1) 
     { 
      //execute insert query here 
      long rows = menus.insert(TABLE_NAME, null, values); 
      return rows; 
      // return rows inserted. 
     } 
     else 
     { 
      //Perform the update query 
      String strFilter = "CHILD_NAME" + optionname; 
      long updaterow=menus.update(TABLE_NAME,values,strFilter,null); 
      return updaterow; 
      // return rows updated. 
     } 

     // menus.close(); 

    } catch (Exception e) { 
     return -1; 
    } 

     finally { 
      if (menus != null) 
       menus.close(); 
     } 
    } 

我的活動:

我轉換整個JSON數據轉換爲字符串對象,然後插入到SQLite的。

String productpage=jsonObject.toString(); 
    db.addmenus(productpage,"Navigationmenus"); 

但它不工作,它不能插入到sqlite中。

任何人都可以解決這個問題很高興欣賞。 在此先感謝

+0

你得到了什麼錯誤?此外,它看起來像使用'TABLE_NAME'和'CHILD_NAME'作爲字符串,而不是它們似乎是常量。 – Nathanael

+0

表名是我的表名,並有兩個列子名稱和標題名稱。我必須將整個json數據轉換爲字符串插入到列標題名稱,我會給一個選項名稱例如。菜單爲一個JSON然後第二個產品選項爲另一個JSON – Nivethitha

+0

您正在存儲JSON到Sqlite?你可能想看看使用Realm作爲你的數據庫,而不是 –

回答

0

您可以參考這個Link。該鏈接解釋瞭如何查找表格中可用的電子郵件地址,您可以更改列名稱,表格並根據值傳遞值。在你的場景中,你想檢查名稱是否已經存在,所以你必須傳遞你想找的名字。如果名稱存在,則此方法將返回true或false。你可以驗證你是否必須插入或更新根據response.i.e。,假意味着你必須插入,否則如果是真的意味着你必須更新。

+0

我使用了數據存在的功能,運行良好。謝謝很多jeeva – Nivethitha

+0

@Nivethitha ::也請回答這個問題。 – Jeevanandhan

0

您可以將用戶insertWithOnConflict()這樣

db.insertWithOnConflict(TABLE, null, yourContentValues, SQLiteDatabase.CONFLICT_REPLACE); 
+0

我想檢查在哪裏conditon選項的名稱,如果已經或不可以。可能有可能insertwithonconflict – Nivethitha

+0

name是用戶主鍵? –

+0

它不適用於我它可以一次又一次插入相同的json數據 – Nivethitha

0

你應該使用更換成

REPLACE INTO table(...) VALUES(...); 
+0

我不想替換table.i只是想更新數據如果數據已經存在於表中使用where condtion name =選項名稱 – Nivethitha

+0

「REPLACE INTO」不替換table.it只是插入或更新數據 –

+0

可以告訴REPLACE INTO查詢正確 – Nivethitha

0

問題並不多清楚,但我想你要檢查或者數據/記錄插入SQLite或不。您需要定義一些額外的變量long rowInserted insert()方法返回新插入的行的行ID,或者在發生錯誤時返回-1。

menus.insert(TABLE_NAME, null, values); 

long rowInserted = db.insert(AddNewPhysicalPerson, null, newValues); 

if(rowInserted != -1) 
    Toast.makeText(myContext, "New row added :" + rowInserted, Toast.LENGTH_SHORT).show(); 
else 
    Toast.makeText(myContext, "Something wrong", Toast.LENGTH_SHORT).show(); 

更新 檢查任一數據是在表或列?對於這一點,你使用這個代碼

public boolean Exists(String id){ 
    Cursor res = getAllData(); 
    int count=0; 
    while (res.moveToNext()){ 
     String email =res.getString(3); 
     if(email.equals(id)){ 
      count++; 
     } 
    } 
    if(count==0){ 
     return false; 
    } else{ 
     return true; 
    } 
} 

第二你問JSON首先將數據存儲在任何列表運行時間,並且它,那麼你可以SQLite中

存儲
try { 
     items = jsonObject.getJSONArray("myjsonattribute"); 
     List<MyAnySetterGetter> mList = new ArrayList<MyAnySetterGetter>(); 
     for (int i = 0; i < items.length(); i++) { 
      JSONObject c = items.getJSONObject(i); 
      String mfilename = c.getString("myjsonattribute2"); 
      mList.add(mfilename); 
      } 
    } catch (JSONException e) { 
     //e.printStackTrace(); 
    } 

然後用上面得到的字符串列表中插入從列表數據的SQLite 像

String str1 = mList.get(position).getMYITEM1(); 
String str2 = mList.get(position).getMYITEM2(); 

插入str1和str2的SQLite中希望你能得到的想法。

+0

正如你上面提到的新行插入到sqlite.But我想只是檢查數據使用where條件名= optionname.if該選項名相應的列空可以插入否則更新 – Nivethitha

+0

確定從數據庫中獲取數據,然後檢查對象返回null或不,如果爲空則更新數據 – Attaullah

+0

存在函數如果工作那麼,如果使用選中列的條件存在或不存在,那麼可以檢查存在去更新否則插入函數。 – Nivethitha

0

你應該爲表

  1. 設置鍵,然後
  2. 插入(如果存在的鍵也不會再插入),然後
  3. 更新所有的行。
相關問題