El siguiente método, cuando se llama con algo como String val = getCell("SELECT col FROM table WHERE LIKE(other_col,'?')", new String[]{"value"});
(esto es SQLite), arroja una java.lang.ArrayIndexOutOfBoundsException: 0 at org.sqlite.PrepStmt.batch(PrepStmt.java:131)
. ¿Alguien puede compadecerse de mi pobre torpeza aquí y ayudarme con por qué ?
/** * Get a string representation of the first cell of the first row returned * by <code>sql</code>. * * @param sql The SQL SELECT query, that may contain one or more '?' * IN parameter placeholders. * @param parameters A String array of parameters to insert into the SQL. * @return The value of the cell, or <code>null</code> if there * was no result (or the result was <code>null</code>). */ public String getCell(String sql, String[] parameters) { String out = null; try { PrepanetworkingStatement ps = connection.prepareStatement(sql); for (int i = 1; i <= parameters.length; i++) { String parameter = parameters[i - 1]; ps.setString(i, parameter); } ResultSet rs = ps.executeQuery(); rs.first(); out = rs.getString(1); rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } return out; }
El setString()
sería, en este caso, ps.setString(1, "value")
y no debería ser un problema. Obviamente estoy equivocado sin embargo.
Muchas gracias de antemano.
Pierde las comillas alnetworkingedor del signo de interrogación. Debería ser LIKE(other_col,?)
. La instrucción preparada descubrirá que tiene una cadena y agregará las comillas.
(¿Realmente le gusta a SQLite LIKE(x,y)
lugar de a un operador x LIKE y
?)