0

I wanted to insert data into my SQLite database. It consists of 3 columns named, id, Questions, and Answer.

This is my addQuestion Method

long addQuestion(addtodb question) {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();

            values.put(KEY_NAME, KEY_NAME); // question Name
            values.put(KEY_ANSWER, KEY_ANSWER); // answer

            // Inserting Row
            return db.insert(TABLE_QUESTIONS, null, values);
            //db.close(); // Closing database connection
        }

         addtodb getQuestion(int id) {
                SQLiteDatabase db = this.getReadableDatabase();

                Cursor cursor = db.query(TABLE_QUESTIONS, new String[] { KEY_ID,
                        KEY_NAME, KEY_ANSWER }, KEY_ID + "=?",
                        new String[] { String.valueOf(id) }, null, null, null, null);
                if (cursor != null)
                    cursor.moveToFirst();

                addtodb question = new addtodb(Integer.parseInt(cursor.getString(0)),
                        cursor.getString(1), cursor.getString(2));

                return question;
            }

I am calling this method in this way,

DatabaseHandler db = new DatabaseHandler(this);
long id = db.addQuestion(new addtodb(0, "Question1", "answer1"));
id = db.addQuestion(new addtodb(0, "Question2", "answer2"));
db.close();

I wanted the records to be 1, Question1, Answer1 Likewise.

But I am getting 1, Question, Answer.

I cannot figure out what went wrong.

3
  • What's your addtodb class look like? Commented Feb 26, 2014 at 14:34
  • please show the declaration of KEY_NAME and KEY_ANSWER Commented Feb 26, 2014 at 14:36
  • you are storing the column names, not their values. see vales.put Commented Feb 26, 2014 at 14:46

3 Answers 3

1

This is where you are doing the mistake, your question that is Question1 is not send, this is send Question

  values.put(KEY_NAME, KEY_NAME); // question Name
  values.put(KEY_ANSWER, KEY_ANSWER); // answer

Change this to

  long addQuestion(String Question, String Answer) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_NAME, Question); // question Name
        values.put(KEY_ANSWER, Answer); // answer

        // Inserting Row
        return db.insert(TABLE_QUESTIONS, null, values);
        //db.close(); // Closing database connection
    }

Your calling method can also change as you are already initating the DB in the addQuestion method

long id = addQuestion("Question1", "answer1");
id = db.addQuestion("Question2", "answer2");

This will work

Sign up to request clarification or add additional context in comments.

Comments

1

I would assume the problem is in these lines:

        values.put(KEY_NAME, KEY_NAME); // question Name
        values.put(KEY_ANSWER, KEY_ANSWER); // answer

You don't pass the actual values to be written into the database. The second parameter of the put method should be the actual value.

        values.put(KEY_NAME, "some question value"); // question Name
        values.put(KEY_ANSWER, "some answer value"); // answer

Comments

0

I am assumnig, your Addtodb class looks like this :

public class Addtodb{

        int id;
        String question;
        String answer;



 public Addtodb(int id, String question, String answer){

     this.id = id;
     this.question = question;
     this.answer = answer;


}



public int getId() {
    return id;
}



public void setId(int id) {
    this.id = id;
}



public String getQuestion() {
    return question;
}



public void setQuestion(String question) {
    this.question = question;
}



public String getAnswer() {
    return answer;
}



public void setAnswer(String answer) {
    this.answer = answer;
}
}

The correction would be here :

long addQuestion(Addtodb question) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_NAME, question.getQuestion()); // question Name
        values.put(KEY_ANSWER, question.getAnswer()); // answer

        // Inserting Row
        return db.insert(TABLE_QUESTIONS, null, values);
        //db.close(); // Closing database connection
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.