8

pgRuby から PostgreSQL と通信するために gem を使用しています。を使用するよりも結果がないかどうかを確認するより良い方法はありres.ntuples == 0ますか?

conn = PGconn.connect config

cmd = "select * from labels inner join labels_mail using(label_id) " + 
  "where labels_mail.mail_id = $1 and labels.name = $2"

res = conn.exec(cmd, [mail_id, mailbox])

if res.ntuples == 0  #  <=== is there a better way to check this?
  cmd = "insert into labels_mail (mail_id, label_id) values ($1, $2)"
  conn.exec(cmd, [mail_id, label_id(mailbox)])
end
4

1 に答える 1

8

ntuples、タイプミス?zero?よりも高速で簡潔に使用できます。== 0

if res.num_tuples.zero?
于 2011-04-20T01:07:18.250 に答える