Updating row with pg => success or failure


#1

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.


#2

Have you tried “row_update.cmd_tuples()”? ntuples() is only for query
results. Use cmd_tuples instead for updates, inserts and deletes.


#3

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
".


#4

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 :slight_smile:

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

thanks!

regards,

r.


#5

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.


#6

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 …

:slight_smile:

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 …

:slight_smile:

regards,

r.


#7

Brian C. 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 …

:slight_smile:

r.