perl, testing a database for a match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl, testing a database for a match
# 1  
Old 05-09-2010
perl, testing a database for a match

hi there.

in perl, I am struggling to find a simple method of connecting to a database, determining if the result of my query is "true" and then testing against the result. I dont even really want the data that i am 'SELECT'íng. i effectively just want to check that a record exists with a UID column (primary key) that exactly matches the $uid that has been passed to my script.

I have managed to fudge a solution together that involves me having to

NOTE: the $uid is passed to script as an argument to the script

1) going to the db and doing "SELECT UID FROM table WHERE UID = $uid"

2) using fetchall_arrayref() to pull the result into an array (although should never be more than one return seeing as the data being requested is a primary key)

3) cycling through the array and sending each value to a fine in /tmp

seeing as this file will only ever contain something or nothing, i do a test later on the script to see if the file has content, if it does, i know that my query was "true" so to speak and I can be sure this a record for the $uid passed to my script in the database


I know that this seems a ridiculously over the top way to do this, but i am relatively new to perl and it was the only way i could get it to work.

Code:
# this value will in reality be passed to this script via a getopts cli argument
my $uid = '1234'     #as an example

my $dbh = DBI->connect("DBI:mysql:DB1","user","pass",) or die $DBI::errstr;
my $sth = $dbh->prepare("SELECT UID FROM table WHERE UID = $uid") or die $DBI::errstr;
       
 $sth->execute or die $DBI::errstr;
        
my $finalref = $sth->fetchall_arrayref();


        
foreach (@$finalref) {
      system("echo @$_ >> /tmp/results");
}

I just wondered if any of the fine gurus on this site could advise me a way of doing a simple 1/2 line version of the above without all the faffing about that ive had to do Smilie. bear in mind, all i need to do is test that a record exists, i dont even need to pull any data from the DB or send any to it at this stage

any help on this would be greatly appreciated
# 2  
Old 05-10-2010
After selecting the data you could build your logic using:

Code:
$sth->rows

You don't need to fetch any data.

P.S. You don't even need real data in the select list:

Code:
select null from table where ...

and then:

Code:
$sth->rows and continue here ... or just stop.

No rows will be evaluated as false in boolean context.
# 3  
Old 05-11-2010
Perl DBI Idiom to get a single cell of data

Code:
my $uid = '1234'     #as an example

my $db = DBI->connect("DBI:mysql:DB1","user","pass",) or die $DBI::errstr;
my $sql= qq~SELECT UID FROM table WHERE UID = $uid~; 

my $rv = $db->selectcol_arrayref( $sql )->[0];

$rv ? print "We Have UID $rv\n" : print "No UID $rv found\n";


Last edited by deindorfer; 05-11-2010 at 03:08 AM.. Reason: Forgot Code Tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl module error in testing

PERL MODULE : To debug my perl module code in test environment. I have taken production module to the test in the my home path directory and was trying to test it by changing the below path in my test code. But still i am getting the error to debug it. can you please let me knw whether i am... (13 Replies)
Discussion started by: ramkumar15
13 Replies

2. Shell Programming and Scripting

PERL: testing directory on windows platform

Hi Gurus, kindly analyse the following for me, please OS: Windows 7 Code location: C:\ Output: "Program Files not being recognised" "System Volume Information is a directory" "Windows not being recognised" main { my @dirlist = <*>; foreach my $fn... (0 Replies)
Discussion started by: biglau
0 Replies

3. Programming

Help with mySQL database by perl script

Hello; I was trying to set up a mysql database using following script, but never went through. The code seems fine without any syntax error as I tested it: perl -c Brapa0101-db.pl Brapa0101-db.pl syntax OKHowever, whenever I run it, an error message was tossed out: DBD::mysql::st execute... (7 Replies)
Discussion started by: yifangt
7 Replies

4. Shell Programming and Scripting

help with perl database printing

Hey guys i am using perl and trying to pull a list of books from a database and then populate the list in a separate TT2 file. When the list is generated there should be 39 book names. When I do the foreach statement in my tt2 below, the first statement gives me 39 Array(random number) and the... (1 Reply)
Discussion started by: Joey12
1 Replies

5. Shell Programming and Scripting

[Perl] script -database

Welcome. I am writing a perl script. I have to design a database consisting of a single table (any subject) saved in a text file. (I make it vi command name and I am giving permission chmod u + x?) The table should contain at least four columns, including a column containing the ID (serial number )... (4 Replies)
Discussion started by: qwerty007
4 Replies

6. Shell Programming and Scripting

parse apl-numeric codes from filenames, and match them to entries in database

Hello, I am new to Unix scripting, and would like some help with my issue: I have vairous files having some alphanumeric codes in them e.g. 10000-01 34440TE 34590SR All these codes are stored in the database, and I need to parse these codes out of these filenames, and match them... (2 Replies)
Discussion started by: mvaidya
2 Replies

7. Shell Programming and Scripting

perl hash of hashes from database

hi there, I have some database output that looks like this SELECT nic_name,nic_duplex,nic_speed,nic_ip FROM network_table WHERE hostname = "server1" result is this (ive delimited with a pipe for ease of reading) bge0|full|1000|10.32.100.1 bge1|full|1000|11.12.101.7 ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

8. Shell Programming and Scripting

Perl Database access

Hi, I tried to run this code but it isnt giving me any output or errors. My aim is to retrieve the row based on the flag name(this is the primary key). flag_test is my table This is how i ran it: perl read_db.pl flag1 flag1 is the criteria in where clause -------- this is my... (2 Replies)
Discussion started by: mercuryshipzz
2 Replies

9. Shell Programming and Scripting

error connecting database from perl

Hi, While i am trying to connect to Oracle database from Perl using DBI module,am getting the error as follows : Can't load '/usr/local/fuseperl-modules/lib/i586-linux-thread-multi/auto/DBD/Oracle/Oracle.so' for module DBD::Oracle: libwtc9.so: cannot open shared object file: No such file... (4 Replies)
Discussion started by: DILEEP410
4 Replies

10. Shell Programming and Scripting

PERL: Read from Database and copy

I have a microsoft access database with anout 220 lines. theres a question and 4 option awnsers and only ones correct which is also listed in th database. Is there any perl script that will copy and the lines from the database and put into a .asp website ? (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question