如何检查Android中是否存在ID?

时间:2014-10-20 06:18:23

标签: java android android-sqlite

我想检查SQLIte DB中是否存在id,然后更新该id,如果SQLite中不存在id,则插入该id。我怎样才能做到这一点。有人可以帮忙如何使用。谢谢感谢。

这是我的活动代码

 public void saveResult()
    {
        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)
            {
                try 
                {
                    DatabaseHelper databaseHelper = new DatabaseHelper(this.getApplicationContext());
                    sqdb = databaseHelper.getWritableDatabase();
                    String strCountQuery = "SELECT question_id, question_type , question_no FROM question_answers where question_id = " + convertVector ; 
                    Cursor cur = sqdb.rawQuery(strCountQuery , null);
                    if (cur.moveToFirst())
                    {
                        int iCount = cur.getCount();
                        System.out.println("iCount = " + iCount);

                        if(cur.getCount() <= 0 )
                        {
                            String strstrqueType = txtViewQuestiontype.getText().toString().trim();
                            String str_que = txtViewQuestion.getText().toString().trim();
                            String str_marks = "1";

                            databaseHelper.insertQueDetails(strQueLimit, strstrqueType, str_que, str_AnswerOptions, str_marks  , strOption_Id);
                            System.out.println("strQueLimit = " + strQueLimit + ", 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 
                        {
                             cur.moveToFirst();  
                             qid = cur.getInt(cur.getColumnIndex("question_id"));
                             String strQue_Type = cur.getString(cur.getColumnIndex("question_type"));
                             String strQue_Nomber = cur.getString(cur.getColumnIndex("question_no"));

                             System.out.println("qid in checkUpdateTable() = " + qid);
                             System.out.println("strQue_Type in checkUpdateTable() = " + strQue_Type);
                             System.out.println("strQue_Nomber in checkUpdateTable()= " + strQue_Nomber);
                             QueId = qid;

                             databaseHelper.updateAnswerRow(qid, str_AnswerOptions);
                        }

                    }

                }
                catch (SQLiteException se )
                {
                    Log.e(getClass().getSimpleName(), "Could not create or Open the database");
                } 

                finally 
                {   if (sqdb != null) { sqdb.close();} }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

您需要在表格上设置约束以触发&#34;冲突&#34;然后通过替换来解决:

CREATE TABLE data   (id INTEGER PRIMARY KEY, question_id  INTEGER, question_type  TEXT, question_no TEXT);
CREATE UNIQUE INDEX data_idx ON data(question_id);

然后你可以发出:

INSERT OR REPLACE INTO data VALUES (NULL, 1, 2, 3);
INSERT OR REPLACE INTO data VALUES (NULL, 2, 2, 3);
INSERT OR REPLACE INTO data VALUES (NULL, 1, 2, 5);