Home > Blockchain >  Android sqlite update not updating as expected, double-quoted string literal or no such column Error
Android sqlite update not updating as expected, double-quoted string literal or no such column Error

Time:04-03

I pass the correct data through to the db modifier function, set the values in the ContentValues, but can not seem to get it to update. I am trying to update by name (a PK) which is of type string, I have done this before with and ID of type int without issues.

here is some code:

companion object {
        private val version = 1
        private val name = "mydatabase.db"
        val TABLE_NAME = "countires"
        val COLUMN_NAME1 = "name"
        val COLUMN_NAME2 = "gold"
        val COLUMN_NAME3 = "silver"
        val COLUMN_NAME4= "bronze"
        val COLUMN_NAME5= "total"

    }
    fun updateCountry(country:Country){
        val db = this.writableDatabase
        val values = ContentValues()
        var trx = country.name
        values.put(COLUMN_NAME1, country.name)
        values.put(COLUMN_NAME2, country.gold)
        values.put(COLUMN_NAME3, country.silver)
        values.put(COLUMN_NAME4, country.bronze)
        values.put(COLUMN_NAME5, country.total)
        db.update(TABLE_NAME,values, "$COLUMN_NAME1=\"$trx\"", null)
        db.close()
    }

In the run second, I get the following message: W/SQLiteLog: (28) double-quoted string literal: "Austria" If I change the query statement to:

        db.update(TABLE_NAME,values, "$COLUMN_NAME1=$trx", null)

I get the following error: (1) no such column: Austria in "UPDATE countires SET bronze=?,silver=?,gold=?,name=?,total=? WHERE name=Austria" D/AndroidRuntime android.database.sqlite.SQLiteException: no such column: Austria (code 1 SQLITE_ERROR): , while compiling: UPDATE countires SET bronze=?,silver=?,gold=?,name=?,total=? WHERE name=Austria at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)...

To be clear there is a column with that name in the DB. A simple search in the db inspector of:

select  *  from countires
where name = "Austria"

Returns the desired row.

There is clearly something wrong with my update command and I am not sure why it does not work. Any help is appreciated.

CodePudding user response:

You should use a ? placeholder for the parameter that you want to pass and use the 4th argument of the method update() for its value inside an array:

db.update(TABLE_NAME, values, "$COLUMN_NAME1 = ?", arrayOf(trx))
  • Related