我想在下一个按钮上更新特定列。但是在运行之后,应用程序会双倍地插入一个值。如果该值已经在表中,我该如何更新。谢谢欣赏。
这是我的数据库代码。
public class DatabaseHelper extends SQLiteOpenHelper
{
SQLiteDatabase sqlDatabase;
//Database name
public static final String DATABASE_NAME = "Test.db";
//Database Version
static final int DATABASE_VERSION = 2;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
this.sqlDatabase = this.getWritableDatabase();
}
public static final String QUESTION_ANSWERS_TABLE = "question_answers";
//Table REGISTRATION_TABLE columns Name
public static final String Question_Id = "question_id";
public static final String Question_No = "question_no";
public static final String Question_Type = "question_type";
public static final String Question = "question";
public static final String Answer = "answer";
public static final String Options_ID = "option";
public static final String Marks = "mark";
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
String CREATE_QUESTION_ANSWERS_TABLE = "CREATE TABLE "+ QUESTION_ANSWERS_TABLE + "("
+ Question_Id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ Question_No + " TEXT,"
+ Question_Type + " TEXT, "
+ Question+ " TEXT, "
+ Answer + " TEXT, "
+ Marks + " TEXT, "
+ Options_ID + " TEXT "+");";
db.execSQL(CREATE_QUESTION_ANSWERS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + QUESTION_ANSWERS_TABLE);
onCreate(db);
}
public boolean insertQueDetails(String strQuestionNo , String strQuestionType, String strQuestion, String strAnswer,
String strMark , String strOptionId )
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Question_No, strQuestionNo);
contentValues.put(Question_Type, strQuestionType);
contentValues.put(Question, strQuestion);
contentValues.put(Answer, strAnswer);
contentValues.put(Marks, strMark);
contentValues.put(Options_ID, strOptionId);
db.insert(QUESTION_ANSWERS_TABLE, null, contentValues);
db.close();
return true;
}
public void updateAnswerRow(Integer strQ_Id , String strAnswer)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues args = new ContentValues();
args.put(Answer, strAnswer);
db.update("question_answers", args, "question_id = ? ", new String[] { Integer.toString(strQ_Id) } );
}
}
这是我的活动代码。
public void checkEmptyTable()
{
DatabaseHelper databaseHelper = new DatabaseHelper(this.getApplicationContext());
sqdb = databaseHelper.getWritableDatabase();
Cursor cur = sqdb.rawQuery("SELECT COUNT(question_id) AS Count_Id FROM question_answers", null);
if (cur != null)
{
cur.moveToFirst();
// Always one row returned.
int Count_Id = cur.getInt(cur.getColumnIndex("Count_Id"));
System.out.println("Count_Id in Table is Empty !!!! i = " + Count_Id); // Zero count means empty table.
}cur.close();
}
public void storingResult()
{
AnswerOptions = (RadioButton) findViewById(Options_RadioGroup.getCheckedRadioButtonId());
String str_AnswerOptions = AnswerOptions.getText().toString().trim();
System.out.println("rbVal = " + str_AnswerOptions);
if (str_AnswerOptions.equals(((Datastructure) Vectore_mquestionDatabaseStructure .get(StaticClass.QuestionNumber)).Answer))
{
if (!StaticClass.isTest)
{
String str_queNo = button_QuestionLimit.getText().toString().trim();
String strstrqueType = txtViewQuestiontype.getText().toString().trim();
String str_que = txtViewQuestion.getText().toString().trim();
String str_marks = "1";
if(Count_Id == 0 )
{
databaseHelper.insertQueDetails(str_queNo, strstrqueType, str_que, str_AnswerOptions, str_marks , strOption_Id);
System.out.println("str_queNo = " + str_queNo + ", strstrqueType = " + strstrqueType +" , str_que = " + str_que + " , str_AnswerOptions = " + str_AnswerOptions + ", str_marks = " + str_marks + ", strOption_Id = " + strOption_Id);
Toast.makeText(this, " Right Answer ", Toast.LENGTH_LONG).show();
}
else if(Count_Id == convertVector)
{
databaseHelper.updateAnswerRow(convertVector , str_AnswerOptions);
Toast.makeText(this, "Updated SuccesFully", Toast.LENGTH_LONG).show();
System.out.println("Answer Updated Successfully !!!");
}
}
}
else
{
if (!StaticClass.isTest)
{
String str_queNo = button_QuestionLimit.getText()
.toString().trim();
String strstrqueType = txtViewQuestiontype.getText()
.toString().trim();
String str_que = txtViewQuestion.getText().toString()
.trim();
String str_marks = "0";
if(Count_Id == 0 )
{
databaseHelper.insertQueDetails(str_queNo, strstrqueType, str_que, str_AnswerOptions, str_marks , strOption_Id);
System.out.println("str_queNo = " + str_queNo + ", strstrqueType = " + strstrqueType +" , str_que = " + str_que + " , str_AnswerOptions = " + str_AnswerOptions + ", str_marks = " + str_marks + ", strOption_Id = " + strOption_Id);
Toast.makeText(this, " Wrong Answer ", Toast.LENGTH_LONG).show();
}
else if(Count_Id == convertVector)
{
databaseHelper.updateAnswerRow(convertVector , strtext);
Toast.makeText(this, "Wrong Updated SuccesFully", Toast.LENGTH_LONG).show();
System.out.println("Wrong Answer Updated Successfully !!!");
}
}
}
if (StaticClass.isTest)
{
StaticClass.resultOfTest = 1 + StaticClass.resultOfTest;
((Datastructure) Vectore_mquestionDatabaseStructure.elementAt(StaticClass.QuestionNumber)).setResult_Of_Test(true);
return;
}
}
这是我的活动屏幕截图
这是屏幕截图
答案 0 :(得分:0)
将要保存到数据库中的所有数据的Getter和setter类生成,并将其用于保存和更新数据。
更改数据库中的更新方法:
public void updateAnswerRow(gettersetterclass classs)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Question_No, classs.getuestionno);
contentValues.put(Question_Type, classs.getquestiontype);
contentValues.put(Question, classs.getquestion);
contentValues.put(Answer, classs.getAnswer);
contentValues.put(Marks, classs.getmarks);
contentValues.put(Options_ID, classs.getoptionid);
db.update(QUESTION_ANSWERS_TABLE , args, Question_Id +" = ? ", new String[] { Integer.toString(classs.getstrQ_Id) } );
}
如果您已经制作了gettersetter类,那么请使用它。