简体   繁体   中英

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 ); 

                    }

The string values should be enclosed inside single quotes:

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);
}

But you should use the recommended and safer method update() with ContentValues and ? placholders:

public void updateName(String newName, String oldName) { // issue is here
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COL_2, newName);
    db.update(TABLE_NAME, cv, COL_2 + "= ?", new String[] {oldName});
    db.close();
}

This way you don't have to worry about adding the single quotes or the risk of sql injection .

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"

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