[SP-pm] contar numero de dados em DBI

Blabos de Blebe blabos at gmail.com
Thu Jul 15 17:07:54 PDT 2010


Wow, cara, RTFM :)

http://search.cpan.org/~timb/DBI-1.611/DBI.pm

Saca só:

http://search.cpan.org/~timb/DBI-1.611/DBI.pm#execute

For a non-SELECT statement, execute returns the number of rows
affected, if known. If no rows were affected, then execute returns
"0E0", which Perl will treat as 0 but will regard as true. Note that
it is not an error for no rows to be affected by a statement. If the
number of rows affected is not known, then execute returns -1.

For SELECT statements, execute simply "starts" the query within the
database engine. Use one of the fetch methods to retrieve the data
after calling execute. The execute method does not return the number
of rows that will be returned by the query *(because most databases
can't tell in advance)*, it simply returns a true value.

http://search.cpan.org/~timb/DBI-1.611/DBI.pm#rows

For SELECT statements, it is generally not possible to know how many
rows will be returned except by fetching them all (...)

One alternative method to get a row count for a SELECT is to execute a
"SELECT COUNT(*) FROM ..." SQL statement with the same "..." as your
query and then fetch the row count from that.

...

Dependendo do tipo da query o retorno do execute muda. A pegadinha é o
que afeta e o que não afeta registros no banco.

Eu to suponto que você está procurando o número de linhas retornadas
para poder paginar, buscando no banco somente um pedaço dos dados
(select blabla from tabela where foo LIMIT 10 OFFSET 80). É isso?


Abraços

2010/7/15 Lucas Moraes <mineiro em live.be>:
> Oi gente, eu queria saber qual a função do DBI para contar elementos no
> select?
>
> no momento estou usando:
>
> COUNT(*)
>
> ou
>
> my $num = $query->execute;
>
> scalar($num);
>
> eu pensei que $num->rows ia retornar, mais não aconteceu nada.
>
> Obrigado.
>
> Deus é o limite!
>
>
> ________________________________
> SMS "HOTMAIL" naar 3010 en ontvang gratis de juiste instellingen voor
> Hotmail op je mobiele telefoon.
> _______________________________________________
> SaoPaulo-pm mailing list
> SaoPaulo-pm em pm.org
> http://mail.pm.org/mailman/listinfo/saopaulo-pm
>


More information about the SaoPaulo-pm mailing list