我一直在尝试更新sqLite数据库中的数据,但是当我点击更新按钮时,它只会返回到上一页,并且没有任何变化,我的代码中的数据没有错误。请检查我的代码谢谢
DatabaseHelper类:
public void updateData(long rowId, 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};
sqLiteDatabase.update(AdminContact.Question.TABLE_QUIZ, contentValues, AdminContact.Question.ID + "=" + rowId, null);
}
my Update 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();
long rowId = 0;
helper.updateData(rowId,question,ans1,ans2,ans3,ans4,sqLiteDatabase);
Toast.makeText(getApplicationContext()," Successfully Updated", Toast.LENGTH_LONG).show();
finish();
}
答案 0 :(得分:0)
我不知道long,但我用这段代码来更新String值,
在DatabaseHelper类中尝试此代码:
sqLiteDatabase.update(AdminContact.Question.TABLE_QUIZ, contentValues, AdminContact.Question.ID + "=" + rowId, null);
而不是:
datetime.date
答案 1 :(得分:0)
我在更新db表的值时遇到了同样的问题。然后我尝试下面的代码。这对我来说可以 试试这段代码。
DatabaseHelper类
public class DatabaseHelper extends SQLiteOpenHelper{
Context context;
// change your database details here;
String db_name = "Database name";
String db_version = "Databse version";
String tbl_name = "Table name";
int col_row_id = "row id col";
String col_question = "Question col name";
String col_ans_1 = "answer 1 col";
String col_ans_2 = "answer 2 col";
String col_ans_3 = "answer 3 col";
String col_ans_4 = "answer 4 col";
public DatabaseHelper(Context context) {
super(context, db_name, null, db_version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "create table " + tbl_name+ "(" +
int_col_row+ " integer primary key autoincrement, " +
col_question+ " text, "+
col_ans_1+ " text, "+
col_ans_2+ " text, "+
col_ans_3+ " text, "+
col_ans_4+ " text, "+
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "drop table if exists "+tbl_name;
db.execSQL(query);
onCreate(db);
}
public void updateData(int rowId, String new_question, String new_ans1, String new_ans2, String new_ans3, String new_ans4) {
String query = "update "+tbl_name+ " set "+
col_question+"=\""+new_question+"\","+
col_answer1+"=\""+answer1+"\","+
col_answer2+"=\""+answer2+"\","+
col_answer3+"=\""+answer3+"\","+
col_answer4+"=\""+answer4+"\""+
" where "+col_row_id+"=\""+rowId+"\"";
SQLiteDatabase db = getWritableDatabase();
db.execSQL(query);
db.close();
}
}
更新课程
public class update{
// initialise New_Question, New_Ans1, New_Ans2, New_Ans3, New_Ans4 as final variables. Also set onclick function of buttion to updateData
public void updateData(View view){
helper = new DatabaseHelper(view.getContext());
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();
int rowId = 0;
helper.updateData(rowId,question,ans1,ans2,ans3,ans4);
}
}