tursodatabase/turso · error · SQLException

Failed to convert into bytes

Error message

Failed to convert {sql} into bytes

What it means

prepare() converts the SQL string to UTF-8 bytes via stringToUtf8ByteArray, which returns null only when the input string is null (UTF-8 encoding of a Java String cannot fail). So this message, despite its generic wording, effectively means prepare(null) — a null SQL string reached statement compilation.

Solutions

  1. Null-check SQL strings before prepare/execute and fail with a meaningful application error
  2. Trace where the null came from — the exception message conveniently embeds the (null) SQL
  3. Default builders to empty or throw early during construction, not at JDBC boundary

Example fix

// before
String sql = queries.get(queryName); // null when key missing
PreparedStatement ps = conn.prepareStatement(sql);

// after
String sql = Objects.requireNonNull(queries.get(queryName), "missing query: " + queryName);
PreparedStatement ps = conn.prepareStatement(sql);
Defensive patterns

Strategy: validation

Validate before calling

// validate before hitting the JDBC boundary
String sql = queries.get(queryName);
Objects.requireNonNull(sql, () -> "SQL for '" + queryName + "' is missing/null");
try (PreparedStatement ps = conn.prepareStatement(sql)) { /* ... */ }

Try / catch

try {
  return conn.prepareStatement(sql);
} catch (SQLException e) {
  if (e.getMessage().startsWith("Failed to convert ")) throw new IllegalArgumentException("null/invalid SQL passed to prepare", e);
  throw e;
}

Prevention

When it happens

Trigger: Connection.prepareStatement(null) or any internal path (createStatement().execute(null), executeInternal with null) that passes a null SQL string; SQL built from a null-valued variable or a map lookup that returned null.

Common situations: SQL read from a config/annotation/map where the key was missing; refactoring left a null default; conditional SQL builder returned null instead of an empty string.

Related errors


AI-assisted analysis of tursodatabase/turso@244cde92a7 (2026-08-20). Data as JSON: /api/errors/4d24a8b8800acce8. Report an issue: GitHub.

Appendix: source

Thrown at bindings/java/src/main/java/tech/turso/core/TursoConnection.java:135

  /**
   * Compiles an SQL statement with optional transaction start check.
   *
   * @param sql An SQL statement.
   * @param checkTransaction Whether to check and start transaction if needed.
   * @return Pointer to statement.
   * @throws SQLException if a database access error occurs.
   */
  private TursoStatement prepare(String sql, boolean checkTransaction) throws SQLException {
    logger.trace("DriverManager [{}] [SQLite EXEC] {}", Thread.currentThread().getName(), sql);

    // Ensure transaction is started if needed (lazy transaction start)
    if (checkTransaction) {
      ensureTransactionStarted(sql);
    }

    byte[] sqlBytes = stringToUtf8ByteArray(sql);
    if (sqlBytes == null) {
      throw new SQLException("Failed to convert " + sql + " into bytes");
    }
    return new TursoStatement(sql, prepareUtf8(connectionPtr, sqlBytes));
  }

  private native long prepareUtf8(long connectionPtr, byte[] sqlUtf8) throws SQLException;

  // TODO: check whether this is still valid for turso
  /**
   * Checks whether the type, concurrency, and holdability settings for a {@link ResultSet} are
   * supported by the SQLite interface. Supported settings are:
   *
   * <ul>
   *   <li>type: {@link ResultSet#TYPE_FORWARD_ONLY}
   *   <li>concurrency: {@link ResultSet#CONCUR_READ_ONLY})
   *   <li>holdability: {@link ResultSet#CLOSE_CURSORS_AT_COMMIT}
   * </ul>
   *
   * @param resultSetType the type setting.

View on GitHub (pinned to 244cde92a7)