Search code examples
javaandroidsqliteandroid-sqlite

Error when trying to update an existing row in sqlite database in Android java?


I'm trying to update the database with the value in edit text. But I get an error in the logger: "*** Process: com.example.coursework2, PID: 12430 android.database.sqlite.SQLiteException: no such column: Wordc (code 1 SQLITE_ERROR): , while compiling: UPDATE Phrases_table SET word = Word WHERE word = Wordc at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

Wordc is the word stored in the database and "Word" is the value im trying to update the database with. I'm new to this, please help.**

// database class, I have only only 1 column which is COL_2

public void updateName(String newName,String oldName) { // issue is here
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL_2 + " = " + newName + " WHERE " + COL_2 + " = " + oldName;
    db.execSQL(query);

/*Edit class, where when I click on the save button, I get the value from the radio button and expect to update the database with the value in the edit text */

 buttonSave.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int pos = listView.getCheckedItemPosition();
                    if (pos > -1) {
                        val = list.get(pos).toString(); //getting value from the radio button which is stored in the database

                       String newWord= edit.getText().toString(); //getting text from edit text


                       myDb.updateName( newWord,val ); 

                    }

Solution

  • You are passing the native query directly, so you need to use quotes on string values:

    String query = "UPDATE " + TABLE_NAME + " SET " + COL_2 + " = \"" + newName + "\" WHERE " + COL_2 + " = \"" + oldName + "\""; db.execSQL(query);

    This will generate the query:

    UPDATE Phrases_table SET word = "Word" WHERE word = "Wordc"