为什么剂量更新会在数据库中创建新ID?

时间:2016-02-04 08:53:43

标签: java android database sqlite sql-update

我正在尝试更新记录,每当我更新记录时,都会使用更新的值生成新的ID。更新应该发生在相同的ID上。可能是什么原因?

创建表格:

 public void createTable(SQLiteDatabase db){
    String CREATE_EVENTS_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_TITLE + " TEXT,"
            + KEY_FROM_DATE + " DATE,"
            + KEY_TO_DATE + " DATE,"
            + KEY_DAY_OF_WEEK + " TEXT,"
            + KEY_LOCATION + " TEXT,"
            + KEY_NOTIFICATION_TIME + " DATE" + ")";

    db.execSQL(CREATE_EVENTS_TABLE);
}

更新功能:

 public int updateEvent(EventData event) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE,event.getTitle());
    values.put(KEY_FROM_DATE,event.getFromDate());
    values.put(KEY_TO_DATE,event.getToDate());
    values.put(KEY_DAY_OF_WEEK,event.getDayOfWeek());
    values.put(KEY_LOCATION,event.getLocation());
    values.put(KEY_NOTIFICATION_TIME,event.getNotificationTime());


    // updating row
    return db.update(TABLE, values, KEY_ID + " = ?",
            new String[] { String.valueOf(event.getId()) });
}

调用更新功能:

    db = new EventTableHelper(getApplicationContext());
        eventData = new EventData();


 db.updateEvent(eventData);

谢谢。

编辑:

我在EventData中的构造函数是:

 public EventData(String title,String fromDate,String toDate,String dayOfWeek,String location,String notificationTime){

    this.title = title;
    this.fromDate = fromDate;
    this.toDate = toDate;
    this.dayOfWeek = dayOfWeek;
    this.location = location;
    this.notificationTime = notificationTime;

}

我正在使用eventData对象添加和更新值:

   db.addEvent(new EventData(eventTitle, startTime, endTime, dayOfWeek, location,notificationTime));

     db.updateEvent(eventData);

id越来越大,我没有将任何值传递给id。

EventData类

    public class EventData  {

    public int id;
    public String title;
    public String fromDate;
    public String toDate;
    public String location;
    public String dayOfWeek;
    public String notificationTime;



    public EventData(){}
    public EventData(String title,String fromDate,String toDate, String location){

        this.title = title;
        this.fromDate = fromDate;
        this.toDate = toDate;
        this.location = location;

    }
    public EventData(int id,String title,String fromDate,String toDate,String dayOfWeek,String location,String notificationTime){

        this.title = title;
        this.fromDate = fromDate;
        this.toDate = toDate;
        this.dayOfWeek = dayOfWeek;
        this.location = location;
        this.notificationTime = notificationTime;

    }
   /* public EventData(String title,String fromDate,String toDate,String dayOfWeek,String location,String notificationTime){

        this.id = id;
        this.title = title;
        this.fromDate = fromDate;
        this.toDate = toDate;
        this.dayOfWeek = dayOfWeek;
        this.location = location;
        this.notificationTime = notificationTime;

    }*/
    public void setId(int id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }

    public void setToDate(String toDate) {
        this.toDate = toDate;
    }


    public void setLocation(String location) {
        this.location = location;
    }


    public void setDayOfWeek(String dayofWeek) {
        this.dayOfWeek = dayofWeek;
    }


    public void setNotificationTime(String notificationTime) {
        this.notificationTime = notificationTime;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getFromDate() {
        return fromDate;
    }

    public String getToDate() {
        return toDate;
    }

    public String getLocation() {
        return location;
    }


    public String getDayOfWeek() {
        return dayOfWeek;
    }

    public String getNotificationTime() {
        return notificationTime;
    }
}

我正在创建具有子视图的事件,因此我想更新我将单击的事件。为此,我使用setTag方法传递视图的id。

这是我的日期片段

    dayplanView = (ViewGroup) view.findViewById(R.id.hoursRelativeLayout);

    int id = i.getIntExtra("id",0);

    mDb = new EventTableHelper(getActivity());
    events = mDb.getAllEvents("Mon");


        int tag = 0;
        for (EventData eventData : events) {


                String datefrom = eventData.getFromDate();

                if (datefrom != null) {
                    String[] times = datefrom.substring(11, 16).split(":");
                    minutesFrom = Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
                }
                String dateTo = eventData.getToDate();

                String title = eventData.getTitle();
                String location = eventData.getLocation();


                if (dateTo != null) {

                    String[] times1 = dateTo.substring(11, 16).split(":");
                    minutesTo = Integer.parseInt(times1[0]) * 60 + Integer.parseInt(times1[1]);
                }

                createEvent(inflater, dayplanView, minutesFrom, minutesTo, title, location, tag);
                tag++;

            }

    return view;
}

private void createEvent(LayoutInflater inflater, ViewGroup dayplanView, int fromMinutes, int toMinutes, String title,String location,int tag) {
    final View eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();

    RelativeLayout container = (RelativeLayout) eventView.findViewById(R.id.container);
    TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle);


    if (tvTitle.getParent() != null)
       ((ViewGroup) tvTitle.getParent()).removeView(tvTitle);

    if(location.equals(""))
    {
        tvTitle.setText("Event : " + title);

    }

    else
    {

        tvTitle.setText("Event : " + title + " (At : " + location +")");

    }

    int distance = (toMinutes - fromMinutes);

    layoutParams.topMargin = dpToPixels(fromMinutes + 9);
    layoutParams.height = dpToPixels(distance);

    eventView.setLayoutParams(layoutParams);
    dayplanView.addView(eventView);
    container.addView(tvTitle);

    eventView.setTag(tag);

    eventView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            i = new Intent(getActivity(),AddEventActivity.class);
            editMode = true;
            i.putExtra("EditMode",editMode);
            int tag = 0;
            tag =(int)v.getTag();
            i.putExtra("tag",tag);
            startActivityForResult(i,1);

        }
    });

}

1 个答案:

答案 0 :(得分:2)

正如我们在评论中所讨论的那样。我认为你不需要像现在这样复杂,

只需这样做,

db.update(TABLE, values, KEY_ID = id, null);

其中,id表示要更新的Integer值或列KEY_ID的值。如果要更新id为5的行,则id的值将为5。