1

I'm having a problem with my codes in updating data. In update class the id is error and I already declare it in DatabaseHelper class but it was still error. Please check my codes.

My DatabaseHelper class:


   public boolean updateData(long id, String new_question, String new_ans1, String new_ans2, String new_ans3, String new_ans4, SQLiteDatabase sqLiteDatabase )
        {
        ContentValues contentValues = new ContentValues();

            contentValues.put(AdminContact.Question.COLUMN_QUESTION, new_question);
            contentValues.put(AdminContact.Question.COLUMN_ANSWER1, new_ans1);
            contentValues.put(AdminContact.Question.COLUMN_ANSWER2, new_ans2);
            contentValues.put(AdminContact.Question.COLUMN_ANSWER3, new_ans3);
            contentValues.put(AdminContact.Question.COLUMN_ANSWER4, new_ans4);

           // String selection = AdminContact.Question.TABLE_QUIZ + " LIKE ?";
            //String[] selection_arg = {new_question};
           int i = sqLiteDatabase.update(AdminContact.Question.TABLE_QUIZ, contentValues, AdminContact.Question.ID + "=" + id, null);
            return i > 0;

        }



    upgrade class:

     public void updateData(View view)
        {
        helper = new DatabaseHelper(this);
            sqLiteDatabase = helper.getWritableDatabase();

            String question, ans1, ans2, ans3, ans4;

            question = New_Question.getText().toString();
            ans1 = New_Ans1.getText().toString();
            ans2 = New_Ans2.getText().toString();
            ans3 = New_Ans3.getText().toString();
            ans4 = New_Ans4.getText().toString();
            helper.updateData(id,question,ans1,ans2,ans3,ans4,sqLiteDatabase);
            Toast.makeText(getApplicationContext()," Successfully Updated", Toast.LENGTH_LONG).show();
            finish();

        }

    }
Jun'z
  • 67
  • 10

3 Answers3

1

Replace Table name with Column name in below line in your code

String selection = AdminContact.Question.TABLE_QUIZ + " LIKE ?";

replace with

String selection = AdminContact.Question.COLUMN_QUESTION + " LIKE ?";
Meenal
  • 2,879
  • 5
  • 19
  • 43
0

You can run simple query on your database using query language. Here's an example.

SQLiteDatabase db;
db.execSQL("create table if not exists "
                + "user"
                + "(_id integer primary key autoincrement, name text not null,  number text not null, "
                + "address text not null, status integer not null, date integer not null)");

Yes, there's many other ways to do database operations. But for beginners, I thought this approach is easier.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Sir Reaz m project can Add, View, Delete and search the only problem is the Update method. – Jun'z Jan 06 '16 at 13:26
  • Try using something like this please. `db.rawQuery("UPDATE "+ TABLE_WORDS + " SET "+ KEY_WORD + " = '"+word.getWord()+"' ,"+KEY_DEFINITION+"= '"+word.getDefinition()+ "' WHERE " + KEY_ID + " = "+word.getID(), null);` – Reaz Murshed Jan 06 '16 at 13:32
  • where should I put these Sir?? – Jun'z Jan 06 '16 at 14:14
  • sqLiteDatabase.rawQuery("UPDATE question_table SET ques = 'new question', ans1 = 'answer 1', ans2 = 'answer 2', ans3 = 'answer3', ans4 = 'answer4' WHERE id = yourid", null) – Reaz Murshed Jan 06 '16 at 18:02
0

looking at you code, you are doing a single update then don't think

String selection = AdminContact.Question.TABLE_QUIZ + " LIKE ?";`

will be is the best way, you should use an id instead, i have modified the code . you can check it out. and I have also commented out a part of the code

public int updateData(String new_question, String new_ans1, String new_ans2, String new_ans3, String new_ans4, SQLiteDatabase sqLiteDatabase ) {

ContentValues contentValues = new ContentValues();
contentValues.put(AdminContact.Question.COLUMN_QUESTION, new_question);
contentValues.put(AdminContact.Question.COLUMN_ANSWER1, new_ans1);
contentValues.put(AdminContact.Question.COLUMN_ANSWER2, new_ans2);
contentValues.put(AdminContact.Question.COLUMN_ANSWER3, new_ans3);
contentValues.put(AdminContact.Question.COLUMN_ANSWER4, new_ans4);

//  String selection = AdminContact.Question.TABLE_QUIZ + " LIKE ?";
//  String[] selection_arg = {new_question};

int count = sqLiteDatabase.update(AdminContact.Question.TABLE_QUIZ,   contentValues, KEY_ROWID + "=" + rowId, null);

return count;
}

I added KEY_ROWID + "=" + rowId where rowId is the id of the particular content you want to update. you can also pass it in via the function

Ibukun Muyide
  • 1,294
  • 1
  • 15
  • 23
  • SIr Ibukin KEY_ROWID + "=" + rowId, is these the name of the Column of the ID?? – Jun'z Jan 06 '16 at 14:15
  • This my Table Sir private static final String CREATE_QUERY = "CREATE TABLE "+ AdminContact.Question.TABLE_QUIZ+"("+ AdminContact.Question.ID+" INTEGER PRIMARY KEY NOT NULL,"+ AdminContact.Question.COLUMN_QUESTION+" TEXT NOT NULL,"+ AdminContact.Question.COLUMN_ANSWER1+" TEXT NOT NULL,"+ AdminContact.Question.COLUMN_ANSWER2+" TEXT NOT NULL,"+ AdminContact.Question.COLUMN_ANSWER3+" TEXT NOT NULL,"+ AdminContact.Question.COLUMN_ANSWER4+" TEXT NOT NULL);"; – Jun'z Jan 06 '16 at 14:16
  • yes, its the row ID, same as AdminContact.Question.ID. when you create a row in a table, it returns an ID (of type lond). you need to pass it in to replace the rowID variable i specified – Ibukun Muyide Jan 06 '16 at 14:49
  • except you didn't pass in the proper ID. it should work because that is what i used in most of my app that requires database update – Ibukun Muyide Jan 07 '16 at 13:11
  • How do you query for a unique row in the database then? its the same id you use for that – Ibukun Muyide Jan 07 '16 at 13:15