Hello, I was wondering if there is an elegant way of seeing if a query
is empty in ruby-dbi. Thank you for your time.
Jacques wrote:
Hello, I was wondering if there is an elegant way of seeing if a query
is empty in ruby-dbi. Thank you for your time.
There are two solutions, both of which are not very elegant:
-
Use COUNT(*) in your SQL statement, to see how many rows are in the
result. Then use select_one to get this number. -
Call method fetch once. If it returns nil, the query was empty.
If you’re using the Postgres DBD, use:
sth = dbh.execute(query)
sth[‘pg_row_count’] # returns the number of rows in the result
Try method StatementHandle#rows, if it returns the number of rows in the
result set. But pay attention that if that’s the case, it’s not the correct
behaviour of that DBD.
Regards,
Michael