Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
conaticus opened this issue Jul 31, 2024 · 1 comment
Labels

Comments

@conaticus
Copy link

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
@conaticus
Copy link
Author

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

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant