return number of rows selected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting return number of rows selected
# 1  
Old 04-05-2006
return number of rows to PERL selected in SQL

Hi all, i am doing a perl script to read from a db. I am able to retrieve the rows, but i am unable to return the number of rows selected. i tried
Code:
$selectedrows = $sth->numrows;

i got the error msg:
Can't locate object method "numrows" via package "DBI::st"
i changed it to
Code:
 $selectedrows = $sth->rows;

i get 0, when i have results returned.

Any advise, thanks.

Last edited by new2ss; 04-05-2006 at 05:22 AM..
# 2  
Old 04-05-2006
Which database driver did you use (the one indicated in the connection string)?
# 3  
Old 01-08-2007
Hi Cbkihong!

I am also facing the same error. Can you please provide the information about what is the difference between $sth->numrows and $sth->rows.
Also I want to know if there is some restriction on the use of $sth->rows as I am also getting 0 even when there are rows in the database.

Kindly guide.

regards
Apoorva Kumar
# 4  
Old 01-08-2007
The easiest way to find out how many rows a query will return is to use the COUNT function in SQL.

For operations that aren't queries (such as INSERTs, UPDATEs, and DELETEs), the do method returns the number of rows affected, -1 when it can't determine the right value, or else undef in case of failure.

Code:
$rows = $dbh->do("DELETE FROM Conference WHERE Language='REBOL'");
if (! defined $rows) {
  # failed, but this is not needed if RaiseError is active
} else {
  print "Deleted $rows rows\n";
}

# 5  
Old 01-08-2007
Dear Anbu!
Thanks for the quick reply. The statment I am using is a Select one. can I use the do method on select statement also?

regards
Apoorva Kumar
# 6  
Old 01-08-2007
do is used to retrieve row counts for other than select.

For example if
Code:
SELECT id,name FROM People WHERE Age > 30

is the query you use then use count to get row count

Code:
SELECT COUNT(*) FROM People WHERE Age > 30

# 7  
Old 01-08-2007
Quote:
Originally Posted by apoorvasharma80
Dear Anbu!
Thanks for the quick reply. The statment I am using is a Select one. can I use the do method on select statement also?
No. This is because no resultset is prepared with the do() method. Please refer to the manual page for DBI (http://search.cpan.org/~timb/DBI-1.53/DBI.pm#do)

Quote:
Prepare and execute a single statement. Returns the number of rows affected or undef on error. A return value of -1 means the number of rows is not known, not applicable, or not available.

This method is typically most useful for non-SELECT statements that either cannot be prepared in advance (due to a limitation of the driver) or do not need to be executed repeatedly. It should not be used for SELECT statements because it does not return a statement handle (so you can't fetch any data).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading and copying a selected rows

Dear All, I have a data file input.res like below. (Only six column shown here for example.) Sequence of first column starting from 1 to 148. Input file 1 Q0 9_August_2014_Entertainment2 0 20.14967806339729 BM25b1.0 1 Q0 13_October_2012_Page323 1 20.134224346765738 BM25b1.0 1 Q0... (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

2. Shell Programming and Scripting

Combine multiple rows based on selected column keys

Hello I want to collapse a file with multiple rows into consolidated lines of entries based on selected columns as the 'key'. Example: 1 2 3 Abc def ghi 1 2 3 jkl mno p qrts 6 9 0 mno def Abc 7 8 4 Abc mno mno abc 7 8 9 mno mno abc 7 8 9 mno j k So if columns 1, 2 and 3 are... (6 Replies)
Discussion started by: linuxlearner123
6 Replies

3. Shell Programming and Scripting

To extract selected rows

Hi, I have two files Input1.txt and Input2.txt and i need to have a output with selected lines starting with the values present in Input2.txt. For example: Input1.txt:... (5 Replies)
Discussion started by: bhas
5 Replies

4. Shell Programming and Scripting

Shell to remove a newline char from selected rows in a file.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (3 Replies)
Discussion started by: mailme0205
3 Replies

5. UNIX for Dummies Questions & Answers

Remove a newline char from selected rows.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (2 Replies)
Discussion started by: mailme0205
2 Replies

6. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

7. Shell Programming and Scripting

Return rows from Oracle package

I need help . I am not getting anything back from my setup. I have defined an oracle package which I am calling from within my sql call, but is returning no rows. If I run my sql from sqlplus works find, does not work whe I try to run from UNIX shell script My korn script test.ksh #!bin/ksh... (6 Replies)
Discussion started by: TimHortons
6 Replies

8. Shell Programming and Scripting

print selected rows with awk

Hi everybody: Could anybody tell me how I can print from a file a selected rows with awk. In my case I only want print in another file all the rows from NR=8 to NR=2459 and the increment each 8 times. I tried to this: awk '{for (i=8; i=2459; i+=8); NR==i}' file1 > file2 But doesn't... (6 Replies)
Discussion started by: tonet
6 Replies

9. Shell Programming and Scripting

flags to suppress column output, # of rows selected in db2 sql in UNIX

Hello, I am new to db2 SQL in unix so bear with me while I try to explain the situation. I have a text file that has the contents of the where condition that I am using for a db2 SQL in UNIX ksh. Here is the snippet. if ; then echo "Begin processing VALUEs" ... (1 Reply)
Discussion started by: jerardfjay
1 Replies

10. Shell Programming and Scripting

Cutting rows after a selected point

I have a log file from which I want to cut out the ten lines assoictiated to my search requirment (cust_ref #). The cust_ref number will be on te first line and the update information will be on the following ten lines (which dosen't linking data to grep on). Using a script I would like to... (4 Replies)
Discussion started by: nhatch
4 Replies
Login or Register to Ask a Question