Forum: Ruby updating row with pg => success or failure

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D48be99143e13e40d1c44a4da63a53f4?d=identicon&s=25 Mongeta 99 (raimon)
on 2009-02-24 18:29
Hello,


I'm playing a little with the pg of Ruby, and I'm accessing some
postgresql database.

I want to update some rows, but I can't find the method/command to know
if they were updated or not.

For a normal select:

 res  = conn.exec('SELECT id, type FROM scanns WHERE ftp=true')
 print 'records found: ',res.ntuples(),"\n"

but when updating, I can't use the ntuples() or at least it returns
always zero.

my rows are updated, I can check it with another tool.

 conn.transaction {
         row_update  = conn.exec('UPDATE scanns SET ftp=false WHERE
id=xxxxx'])
         print 'records found updating: ',row_update.ntuples(),"\n",
"\n"

         }

wich command I have to use ?

I have the latest RDoc documentation, but can't find it ...

thanks,

r.
69f776698d1a63c70af26bfaea085b17?d=identicon&s=25 Choi, Junegunn (Guest)
on 2009-02-24 19:20
(Received via mailing list)
Have you tried "row_update.cmd_tuples()"? ntuples() is only for query
results. Use cmd_tuples instead for updates, inserts and deletes.
D48be99143e13e40d1c44a4da63a53f4?d=identicon&s=25 Mongeta 99 (raimon)
on 2009-02-25 08:07
Choi, Junegunn wrote:
> Have you tried "row_update.cmd_tuples()"? ntuples() is only for query
> results. Use cmd_tuples instead for updates, inserts and deletes.

From the RDoc:
---------------------------------------------------------
res.cmd_tuples() → Fixnum
Returns the number of tuples (rows) affected by the SQL command.

If the SQL command that generated the PGresult was not one of:

INSERT
UPDATE
DELETE
MOVE
FETCH
or if no tuples were affected, 0 is returned.

Actually, my SQL involved an UPDATE statement, and for what I
understand, this command is only usefull for SELECT statements ?

But as you suggest, it's working and I'm getting the number of rows
affected for my update :-)

are the docs wrong, I'm understanding them in the wrong way ?

thanks!

regards,

r.
69f776698d1a63c70af26bfaea085b17?d=identicon&s=25 Choi, Junegunn (Guest)
on 2009-02-25 08:54
(Received via mailing list)
I don't see any problem in the doc. Maybe you misread the sentence?:
"If the SQL command that generated the PGresult was **NOT ONE OF** ...
".
D48be99143e13e40d1c44a4da63a53f4?d=identicon&s=25 Mongeta 99 (raimon)
on 2009-02-25 10:46
Choi, Junegunn wrote:
> I don't see any problem in the doc. Maybe you misread the sentence?:
> "If the SQL command that generated the PGresult was **NOT ONE OF** ...

Yes, I read and read and re-read ...

:-)

And I still think the same:

The result of res.cmd_tuples()  is valid if the command that generated
it IT'S NOT ONE OF THIS:

INSERT
UPDATE
DELETE
MOVE
FETCH


I would say it in this way:

The result of res.cmd_tuples()  is valid if the command that generated
it it's one of this:

INSERT
UPDATE
DELETE
MOVE
FETCH

English is not my nativa language and I can be wrong, but at least this
is what I understand ...

:-)

regards,

r.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-02-25 16:35
Raimon Fs wrote:
> Choi, Junegunn wrote:
>> I don't see any problem in the doc. Maybe you misread the sentence?:
>> "If the SQL command that generated the PGresult was **NOT ONE OF** ...
>
> Yes, I read and read and re-read ...

Perhaps it's clearer if turned around:

0 is returned if the command is not one of (insert, update, delete,
move, fetch), or if no tuples were affected.

Or negate it:

A non-zero value is returned if the command is one of (insert, update,
delete, move, fetch) and one or more tuples were affected.
D48be99143e13e40d1c44a4da63a53f4?d=identicon&s=25 Mongeta 99 (raimon)
on 2009-02-25 17:31
Brian Candler wrote:
> Raimon Fs wrote:
>> Choi, Junegunn wrote:
>>> I don't see any problem in the doc. Maybe you misread the sentence?:
>>> "If the SQL command that generated the PGresult was **NOT ONE OF** ...
>>
>> Yes, I read and read and re-read ...
>
> Perhaps it's clearer if turned around:
>
> 0 is returned if the command is not one of (insert, update, delete,
> move, fetch), or if no tuples were affected.
>
> Or negate it:
>
> A non-zero value is returned if the command is one of (insert, update,
> delete, move, fetch) and one or more tuples were affected.

thanks !

that's more clear ...

:-)

r.
This topic is locked and can not be replied to.