在数据库中插入行,检查它是否存在

时间:2013-11-27 14:31:23

标签: android sql database

我在我的数据库助手中有代码来检查是否存在特定的地方,如果这个项目存在,它将显示该地方已经存在的吐司,如果不存在,它将把这个地方添加到表中。它只检查它是否存在并显示toast消息,否则它不会在表中插入地名如果它不存在..我不知道为什么我的插入代码是正确的但是当添加if语句时它只检查是否这存在。

这是我在数据库助手中的查询:

 public boolean CheckBookmarks(String i) {
    // TODO Auto-generated method stub
    boolean booked;
    Cursor c = mDataBase.rawQuery("SELECT _id,facility_id from bookmark where facility_id ='"+i+"'", null);

    String result;
    int iId=c.getColumnIndex(DB_Facility_ID);
          c.moveToFirst();  
          result=c.getString(iId);

          if(c.isNull(iId))

              booked=false;
          else 
              booked=true;

    return booked ;
}

我也在数据库帮助器中尝试了这个代码,而id没有用:

public boolean CheckBookmarks(String i) {
    // TODO Auto-generated method stub

    Cursor c = mDataBase.rawQuery("SELECT _id,facility_id from bookmark where facility_id ='"+i+"'", null);

    String result;


    int iId=c.getColumnIndex(DB_Facility_ID);

          c.moveToFirst();  


result=c.getString(iId);

    return true ;

这是我活动中的代码:

 if(dbhelper.CheckBookmarks(namee))
        Toast.makeText(getApplicationContext(),
                "Place is already in Bookmarks ", Toast.LENGTH_LONG)
            .show();


    //if(!dbhelper.CheckBookmarks(namee)){
    else{
    ContentValues cvalues = new ContentValues();
    cvalues.put("facility_id", namee);

    long data =dbhelper.insertbookmark(cvalues);
     Toast.makeText(getApplicationContext(),
                "Place added to Bookmarks ", Toast.LENGTH_LONG)
            .show();

    }

0 个答案:

没有答案