gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:58:20 25 4
gpt4 key购买 nike

我正在尝试更新记录,每当我更新记录时,都会使用更新后的值生成新的 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;
}
}

我正在使用 subview 创建事件,因此我想更新我将单击的事件。为此,我使用 setTag 方法来传递 View 的 id。

这是我的一天 fragment

    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);

}
});

}

最佳答案

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

只要这样做,

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

其中 id 表示整数值或您要更新的列 KEY_ID 的值。就像如果要更新 id 为 5 的行,则 id 的值为 5。

关于java - 为什么剂量更新会在数据库中创建新的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35196447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com