2016-05-26 8 views
0

私は、外部キーを使用して親テーブルから値を取得することに問題があります。私は2つのテーブルを使用していアンドロイドの外部キーを使用して親テーブルから値を取得する方法は?

1)質問表の質問表のフィールドに

2)質問表

は、questionIdquestionrequiredTimeとなります。回答テーブルのフィールドに は、questionIdの値をフェッチし、回答テーブルに挿入する方法を質問表では、

私の問題がある を(質問表から参照している外部キー)answerIdanswerquestionIdのですか?

1つの質問には4つの回答があります。質問ごとに4つのオプションがあります。

//Create Table Question 
    public static final String CREATE_QUESTION = "CREATE TABLE IF NOT EXISTS " 
      + TABLE_QUESTION 
      + " (" 
      + KEY_QUESTION_ID 
      + " INTEGER primary key not null , " 
      + KEY_QUESTION 
      + " text not null , " 
      + KEY_TIME_REQUIRED + " text " + ");"; 


    //Create Table Answer 
    public static final String CREATE_ANSWER = "CREATE TABLE IF NOT EXISTS " 
      + TABLE_ANSWER 
      + " (" 
      + KEY_ANSWER_ID 
      + " INTEGER primary key not null , " 
      + KEY_ANSWER 
      + " text , " 
      + KEY_QUESTION_ID 
      + " INTEGER ," 
      + " FOREIGN KEY (" + KEY_QUESTION_ID + ") REFERENCES " + TABLE_QUESTION + " (" + KEY_QUESTION_ID + "));"; 


In Code, 

        for (int i = 0; i < arr_question_id.size(); i++) { 

          item_bean.setTimeRequired(arr_time_required.get(i)); 
          item_bean.setQuestionId(arr_question_id.get(i)); 
          item_bean.setQuestion(arr_question.get(i)); 

          dbAdapter.openForWrite(); 
          dbAdapter.insertQuestion(Integer.parseInt(arr_question_id.get(i)), arr_question.get(i), arr_time_required.get(i)); 
          dbAdapter.close(); 
         } 


         for (int j = 0; j < arr_answer_id.size(); j++) { 
          item_bean.setAnswerId(arr_answer_id.get(j)); 
          item_bean.setAnswer(arr_answer.get(j)); 
          item_bean.setCorrect(arr_correct.get(j)); 

          System.out.println("!!!!!!!!!!1 Integer.parseInt(arr_question_id.get(j)=====" + json_object_question.getString("questionID")); 

          dbAdapter.openForWrite(); 
          dbAdapter.insertAnswer(Integer.parseInt(arr_answer_id.get(j)), arr_answer.get(j), Integer.parseInt(json_object_question.getString("questionID"))); 
          dbAdapter.close(); 
         } 

Problem with below Line, 
dbAdapter.insertAnswer(Integer.parseInt(arr_answer_id.get(j)), arr_answer.get(j), Integer.parseInt(json_object_question.getString("questionID"))); 

For Question Id,For one Question loop repeat 4 times. 


Table Question 
*************** 
questionId question     timeRequired 
1   What is your Name?   10 
2   What is your pets Name?  10 
3   What is your native?  10 
etc. 

Table Answer 
*********** 
answerId  answer  questionId 
101   Neha   1 
102   Sneha  1 
103   Meeta  1 
104   Mamata  1 

105   Meetu  2 
106   Keenu  2 
107   Teenu  2 
108   Nehu   2 

etc. 

私はテーブル回答のquestionIdの値を取得できません。 私を助けてください。 私はあなたの回答とあなたの提案を待っています。 お願いします。おかげさまで リーナ・あなたがほとんどすべてを行っていた

+0

Answerテーブルに外部キーの参照を使用する必要はありません。だから、私はあなたが外来キーの参照を削除することをお勧めします。 –

+0

@jaydroider実装方法。私はarraylistを取って、すべての値と質問テーブルと回答テーブルのsqliteデータベースにarraylistストアを格納します – Reena

+0

'Answerテーブル'から外部キーの参照を削除します。回答テーブルにのみ整数を保持し、回答テーブルにデータを追加している間は、質問IDの条件を指定します。 –

答えて

0

、それはあなたが望むことをquestionIdと回答表から選択クエリだけの問題である:

SQLiteDatabase db = this.getWritableDatabase(); 

String[] columns = new String[] { "answer" }  
String where = KEY_QUESTION_ID + " = ?"; 
String[] whereArgs = new String[] { questionId + "" }; 

Cursor c = db.query(
    TABLE_ANSWER /* table */, 
    columns /* columns */, 
    where /* where or selection */, 
    whereArgs /* selectionArgs i.e. value to replace ? */, 
    null /* groupBy */, 
    null /* having */, 
    null /* orderBy */ 
); 
+0

しかし、私は回答テーブルにレコードを挿入したい – Reena

+0

あなたの回答テーブルはそれ自身のPKを持っているか、例えば質問テーブル(例えば、+ quetionId)を組み合わせたPKを持っていなければなりません。 –

+0

あなたの質問が表示されない場合は、もう少し詳しく説明して、シナリオを挿入して説明してください。 –

0

あなたが加入またはビューを作成使用して、必要な結果を得ることができます。それはあなたの必要性にかかっています。 http://www.tutorialspoint.com/sqlite/sqlite_using_joins.htm

他にも制約がありますが、それ以外の場合は質問がないと答えないのはなぜですか?