Search code examples
androidsqliteormlite

How update property with single quotes on ORMLite?


When I try update a object with a property that contains single quote, happens error type: near "Agua": syntax error

The value of property is: "D'Agua"

But the orm lite make the sql with single quote as example:

UPDATE 'table' SET 'value' = 'D'Agua'

Result of code:

UpdateBuilder<Table, Integer> updateBuilder = tableDAO.updateBuilder();
try {
  updateBuilder.updateColumnValue("value", tableDTO.getDescricao());
  updateBuilder.update();
} catch (SQLException e) {
  e.printStackTrace();
}

Solution

  • When I try update a object with a property that contains single quote, happens error type: near "Agua": syntax error

    This is a FAQ. Anytime you might have special characters in your statements, you should leverage the SQL ? arguments. With ORMLite, you use the SelectArg class. Sort of an unfortunate name given that its an update.

    SelectArg selectArg = new SelectArg(tableDTO.getDescricao());
    updateBuilder.updateColumnValue("value", selectArg);
    updateBuilder.update();
    

    This will result in the SQL:

    UPDATE 'table' SET 'value' = ?
    

    Then the string "D'Agua" will be passed into the update statement as an argument.

    More on this via a search for "quotes in queries" in the online docs.