how to print out data from mysql table in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to print out data from mysql table in perl script
# 1  
Old 12-22-2010
how to print out data from mysql table in perl script

I'm having trouble with this code.

if i do

.\read.pl -u user

it prints out

2010-12-20 12:00:00 host1 <cmd>a
2010-12-20 12:00:01 host1 <cmd> <execute>
2010-12-20 12:00:02 host1 <cmd>b
2010-12-20 12:00:03 host1 <cmd>c

however, if i enter
.\read.pl -h host1

it should print out
2010-12-20 12:00:00 user <cmd>a
2010-12-20 12:00:01 user <cmd> <execute>
2010-12-20 12:00:02 user <cmd>b
2010-12-20 12:00:03 user <cmd>c

and when i use this option
.\read.pl -u user -h host1

it prints out
2010-12-20 12:00:00 host1 <cmd>a
2010-12-20 12:00:01 host1 <cmd> <execute>
2010-12-20 12:00:02 host1 <cmd>b
2010-12-20 12:00:03 host1 <cmd>c

when i want it to print out
2010-12-20 12:00:00 <cmd>a
2010-12-20 12:00:01 <cmd> <execute>
2010-12-20 12:00:02 <cmd>b
2010-12-20 12:00:03 <cmd>c

can someone help me? can i not use the !defined() method? Thanks.

Code:
 
# DBI connection statement
my $dbh = DBI->connect("DBI:mysql:$database:$host", $user, $pw, {RaiseError=>1}) or die $DBI::errstr;
# Command line options
our ($opt_u, $opt_h, $opt_s, $opt_e, $opt_c);
getopt('u:h:s:e:c');
# Different queries
my $query;
my $sth;
if (defined($opt_u) && !defined($opt_h && $opt_s)) {
$query = ('SELECT time, host, cmd FROM '.$table.' WHERE user = "'.$opt_u.'"');
$sth = $dbh->prepare($query);
$sth->execute();
while (@ary = $sth->fetchrow_array()) {
foreach(@ary) {
print "$_ ";
}
print "\n";
}
}
elsif (defined($opt_h) && !defined($opt_u && $opt_s)) {
$query = ('SELECT time, user, cmd FROM '.$table.' WHERE host = "'.$opt_h.'"');
$sth = $dbh->prepare($query);
$sth->execute();
while (@ary = $sth->fetchrow_array()) {
foreach(@ary) {
print "$_ ";
}
print "\n";
}
}
elsif (defined($opt_u && $opt_h) && !defined($opt_s)) {
$query = ('SELECT time, cmd FROM '.$table.' WHERE user = "'.$opt_u.'" AND host = "'.$opt_h.'"');
$sth = $dbh->prepare($query);
$sth->execute();
while (@ary = $sth->fetchrow_array()) {
foreach(@ary) {
print "$_ ";
}
print "\n";
}
}

---------- Post updated 12-22-10 at 02:06 PM ---------- Previous update was 12-21-10 at 02:44 PM ----------

can someone help please

Last edited by pludi; 12-23-2010 at 08:39 AM..
# 2  
Old 12-22-2010
Simple logic flaws -- test both options every time, or be careful which is first. You can leave out terms if the possibilities have been pruned. Ever study the karnaugh diagram of 4 bit logic minimalization?

Karnaugh map - Wikipedia, the free encyclopedia
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 12-22-2010
thank you. no i did not study that.
# 4  
Old 12-22-2010
I almost never use else if, going for switch/case or if case a then (process A; exit or return) fi If case b then (process B,exit or return) fi It makes the selection more obvious, and does not indent a lot. But the brute force solution is to test both every time, so there is only one block executed for each of: ab, a not b, b not a, not a not b.

Think of h, u and s as bits 2^0, 2^1, 2^2, respectively and add them for each case if true. Decide what you want to happen for each of the 8 cases.

Your logic operators and defined/!defined may not be doing what you think.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Send Data to MySQL Table Columns

I have two scripts, each script reads an individual data file and copies specific lines of data and sends to MySQL table. Only difference is, each script sends data to a separate column on the same DB. I want to use one script to populate DB table and have data look horizontal, with no overlapping.... (3 Replies)
Discussion started by: SysAdminRialto
3 Replies

2. Programming

MYSQL merge csv data with exisiting table

I have a MYSQL table with demographic data. The addresses contained in the table were entered manually before the advent of online postcode lookup software and, hence, there are a lot of errors when using automated online mailing checking software. I plan to export the data as csv file for a 3rd... (1 Reply)
Discussion started by: barrydocks
1 Replies

3. Shell Programming and Scripting

Need to Print output in table using shell script

#! /bin/ksh #] && . ./.profile 2>/dev/null if test -f '.profile'; then . ./.profile; fi; #. .profile LOG_DIR=/app/rpx/jobs/scripts/just/logs sendEmail() { pzCType="$1"; pzTitle="$2"; pzMsg="$3"; pzFrom="$4"; pzTo="$5"; pzFiles="$6"; pzReplyTo="$7" ( ... (4 Replies)
Discussion started by: ankit.mca.aaidu
4 Replies

4. Shell Programming and Scripting

Input data of a file from perl into HTML table

Hi , I need an help in perl scripting. I have an perl script written and i have an for loop in that ,where as it writes some data to a file and it has details like below. cat out.txt This is the first line this is the second line. .....Now, this file needs to be send in mail in HTML... (2 Replies)
Discussion started by: scott_cog
2 Replies

5. Shell Programming and Scripting

Shell script for adding a table in mysql with 10,000 lines ... pls help

Hi , I am new to shell scripting . I need to write a shell script in sql to add 10,000 lines of data in a table . Pls help guys :) ---------- Post updated at 07:08 PM ---------- Previous update was at 03:40 PM ---------- guys please help !!! (3 Replies)
Discussion started by: vinumahalingam
3 Replies

6. Shell Programming and Scripting

Help with perl script to output data in table format...

Hello, I need help with a perl script that will process a text file and match virtual server name to profile(s). the rest will be ignored. Virtual server name follows the word "virtual" in the begging of the line. There could be multiple profiles assigned to one virtual server. For example, ... (3 Replies)
Discussion started by: besogon
3 Replies

7. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

8. Shell Programming and Scripting

How to scan data directly from Table using a script

Hi, I have a new task where i have two tables Acct_ open and lookup table In the Acct_open table there is all the information about an account including account number and lookup table is having a country code and corresponding country name where that account has been opened. both... (2 Replies)
Discussion started by: manmeet
2 Replies

9. Shell Programming and Scripting

Help Inserting data in mysql table

Cant understand the error #!/bin/bash temp="" A="" D=$(date +"%Y-%m-%d") H=$(date +"%R") temp=$(wget -q -O - website | grep -o "Temperature:]**" | grep \-E -o "+") mysql -D "weather_wise" -e "INSERT INTO weather (Date, Hour, Degrees) VALUES ($D,$H, $temp)"; my data types for... (11 Replies)
Discussion started by: vadharah
11 Replies
Login or Register to Ask a Question