recursive_acronym
recursive_acronym

Reputation: 3031

Ruby DBI - How to check if a recordset is empty

Using Ruby DBI, how can I check if a record set is empty? (without iterating through it or doing a count query)

sth = dbh.prepare("select * from things WHERE created_at > '#{start}'")
sth.execute

Upvotes: 0

Views: 709

Answers (1)

tadman
tadman

Reputation: 211670

You could always just ask the result object:

res = sth.execute
res.num_rows

The operation will have to pull down all matching records, though, so if you only need a count, you might want to select that directly.

Also escape your SQL. You cannot just put arbitrary strings in there. This is better:

sth = dbh.prepare("select * from things WHERE created_at > '%s'" % sth.escape_string(start))

Upvotes: 1

Related Questions