xerial/sqlite-jdbc

Prepared Statement + RETURN_GENERATED_KEYS throws "Query returns results" exception when calling executeUpdate()

conaticus opened this issue · 1 comments

Bug Description
When executing a prepared statement, and adding the "RETURN_GENERATED_KEYS" flag, if I call executeUpdate(), for that query, I get this error in the console: java.sql.SQLException: Query returns results. This is due to the fact I have a RETURNING statement, as with the RETURNING removed it works and inserts the item into the database. Desirably I should be able to run my insert and then return the id, but this doesn't seem possible.

This is the code:

        try {
            var sql = "INSERT INTO claims (owner_id, x0, y0, z0, x1, y1, z1, min_height, max_height) VALUES  (?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id;";
            var stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

            stmt.setString(1, "test");
            stmt.setInt(2, 20);
            stmt.setInt(3, 20);
            stmt.setInt(4, 20);
            stmt.setInt(5, 20);
            stmt.setInt(6, 20);
            stmt.setInt(7, 20);
            stmt.setInt(8, 20);
            stmt.setInt(9, 20);

            stmt.executeUpdate(); // <-- Exception occurs here
            System.out.println(stmt.getGeneratedKeys().getInt("id"));
        } catch (SQLException e) {
           System.out.println(e);
        }

Unsure this is a bug or if I am doing something wrong here? I attempted this with executeQuery() instead, however, that did not throw an error or insert into the database. Additionally, removing the RETURN_GENERATED_KEYS flag also results in the Query returns results error.

Schema For Reproduction

CREATE TABLE IF NOT EXISTS claims (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    owner_id VARCHAR(36) NOT NULL, -- uuid

    x0 INTEGER NOT NULL,
    y0 INTEGER NOT NULL,
    z0 INTEGER NOT NULL,

    x1 INTEGER NOT NULL,
    y1 INTEGER NOT NULL,
    z1 INTEGER NOT NULL,

    min_height INTEGER NOT NULL,
    max_height INTEGER NOT NULL
);

Environment

  • OS: Windows 10
  • CPU architecture: x86
  • sqlite-jdbc version: 3.46.0.0
  • sqlite driver version: 3.45.1

Just realised that the RETURNING statement is uneeded because of the return generated keys flag. This was a mistake on my part.