Question

Suppose I have the following SQL table definition:

create table test(
    id number(5) primary key
);

Furthermore, I'd like to execute INSERT and DELETE statements on said table by using Groovy's SQL support.

Given the following code, which executes an INSERT statement:

Sql db = new Sql(some_dataSource)
String query = "insert into test (id) values (999)"
def result = db.execute(query)
  • Can I know, inside this code, how many rows were inserted into test (in this case, 1)?

  • Is the result variable useful in determining this?

I've tried looking at Groovy's SQL support documentation here and here, but the examples provided only seem to count rows over SELECT statements.

Thanks!

Was it helpful?

Solution

You can get the number of rows inserted with Sql.getUpdateCount(). Example:

db.execute(query)
println db.updateCount

The return value from execute is a boolean indicating whether the statement was successfully executed.

The API docs for Sql.execute() have a complete explanation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top