私は、外部キーを使用して親テーブルから値を取得することに問題があります。私は2つのテーブルを使用していアンドロイドの外部キーを使用して親テーブルから値を取得する方法は?
、
1)質問表の質問表のフィールドに
2)質問表
は、questionId
、question
requiredTime
となります。回答テーブルのフィールドに は、questionIdの値をフェッチし、回答テーブルに挿入する方法を質問表では、
私の問題がある を(質問表から参照している外部キー)answerId
、answer
とquestionId
のですか?
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の値を取得できません。 私を助けてください。 私はあなたの回答とあなたの提案を待っています。 お願いします。おかげさまで リーナ・あなたがほとんどすべてを行っていた
Answerテーブルに外部キーの参照を使用する必要はありません。だから、私はあなたが外来キーの参照を削除することをお勧めします。 –
@jaydroider実装方法。私はarraylistを取って、すべての値と質問テーブルと回答テーブルのsqliteデータベースにarraylistストアを格納します – Reena
'Answerテーブル'から外部キーの参照を削除します。回答テーブルにのみ整数を保持し、回答テーブルにデータを追加している間は、質問IDの条件を指定します。 –