如果已经存在更新或者在android中插入,如何在SQLite中检查数据

时间:2016-04-28 05:01:16

标签: android android-layout android-fragments android-sqlite android-studio-2.0

我想检查SQLite中的数据,如果已经存在可以更新或者插入。我正在检查这样的代码,我将在下面提到。

代码:

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中。

有人解决这个问题很高兴欣赏。   提前致谢

5 个答案:

答案 0 :(得分:0)

您可以像这样使用insertWithOnConflict()

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

答案 1 :(得分:0)

你应该使用替换

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

答案 2 :(得分: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首先在任何List运行时存储所有数据并从中获取字符串然后你能够存储在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();

在SQLite中插入str1和str2希望你能理解。

答案 3 :(得分:0)

您可以参考此Link。该链接说明了如何查找表中可用的电子邮件地址,您可以更改列名,表并根据值传递值。在您的方案中,您要检查名称是否已存在,因此您必须传递要查找的名称。如果名称在那里,则此方法将返回true或false。您可以验证是否必须根据response.i.e进行插入或更新。,false表示您必须插入,否则如果为true表示您必须更新。

答案 4 :(得分:0)

你应该

  1. 为表格设置密钥,然后
  2. insert(如果密钥存在则不再插入),然后
  3. 更新所有行。