简体   繁体   中英

Inserting data into SQLite

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.

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

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

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM