gpt4 book ai didi

安卓:更新数据

转载 作者:行者123 更新时间:2023-11-30 01:39:51 25 4
gpt4 key购买 nike

我的代码在更新数据时遇到问题。在更新类中,id 是错误的,我已经在 DatabaseHelper 类中声明了它,但它仍然是错误的。请检查我的代码。

My DatabaseHelper class:


public boolean updateData(long id, String new_question, String new_ans1, String new_ans2, String new_ans3, String new_ans4, SQLiteDatabase sqLiteDatabase )
{
ContentValues contentValues = new ContentValues();

contentValues.put(AdminContact.Question.COLUMN_QUESTION, new_question);
contentValues.put(AdminContact.Question.COLUMN_ANSWER1, new_ans1);
contentValues.put(AdminContact.Question.COLUMN_ANSWER2, new_ans2);
contentValues.put(AdminContact.Question.COLUMN_ANSWER3, new_ans3);
contentValues.put(AdminContact.Question.COLUMN_ANSWER4, new_ans4);

// String selection = AdminContact.Question.TABLE_QUIZ + " LIKE ?";
//String[] selection_arg = {new_question};
int i = sqLiteDatabase.update(AdminContact.Question.TABLE_QUIZ, contentValues, AdminContact.Question.ID + "=" + id, null);
return i > 0;

}



upgrade class:

public void updateData(View view)
{
helper = new DatabaseHelper(this);
sqLiteDatabase = helper.getWritableDatabase();

String question, ans1, ans2, ans3, ans4;

question = New_Question.getText().toString();
ans1 = New_Ans1.getText().toString();
ans2 = New_Ans2.getText().toString();
ans3 = New_Ans3.getText().toString();
ans4 = New_Ans4.getText().toString();
helper.updateData(id,question,ans1,ans2,ans3,ans4,sqLiteDatabase);
Toast.makeText(getApplicationContext()," Successfully Updated", Toast.LENGTH_LONG).show();
finish();

}

}

最佳答案

在代码的下一行用列名替换表名

String selection = AdminContact.Question.TABLE_QUIZ + " LIKE ?";

替换为

String selection = AdminContact.Question.COLUMN_QUESTION + " LIKE ?";

关于安卓:更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34633785/

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