Jesús Zazueta
Jesús Zazueta

Reputation: 1192

Counting affected rows using Groovy SQL

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)

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!

Upvotes: 2

Views: 4038

Answers (1)

ataylor
ataylor

Reputation: 66069

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.

Upvotes: 5

Related Questions