perl DBI: populate a scalar from a select statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl DBI: populate a scalar from a select statement
# 1  
Old 11-17-2009
perl DBI: populate a scalar from a select statement

hi

every resource i see regarding DBI refers to retrieving data from a database into and array or a hash, but i havent seen anything on how to pull out a single value to a scalar

in my database i have a field called "forcewrite" with a value of "6". I am trying to connect to the database, pull this value straight out to a scalar variable, so i can evaluate it later... is there a way to do this ..?

I tried this but this just returns a value of "1" everytime i run it (im assuming it must be an error code or something because im not sure where its getting the "1" from)


Code:
#!/usr/bin/perl -w
use DBI;
my $primac = "6666.5555.4444";
my $dbh = DBI->connect("DBI:mysql:myDB","username","password",) or die $DBI::errstr;
my $force = $dbh->do("SELECT forcewrite FROM table WHERE primac = '$primac'") or die $DBI::errstr;
print "$force\n";

Is there a way to pull a value from a DB straight into a scalar ?

---------- Post updated at 08:17 PM ---------- Previous update was at 08:06 PM ----------

ive managed to get it working this way ...just would be nice if i could go straight to a scalar as opposed to the first element of an array... oh well

Code:
#!/usr/bin/perl -w
use DBI;
my $primac = "6666.5555.4444";
my $dbh = DBI->connect("DBI:mysql:myDB","username","password",) or die $DBI::errstr;
my $sth = $dbh->prepare("SELECT forcewrite FROM admin WHERE primac = '$primac'") or die $DBI::errstr;
$sth->execute() or die  $DBI::errstr;
my $force = ($sth->fetchrow_array())[0];
print "$force\n";

# 2  
Old 11-17-2009
Try this:
Code:
my $force;

my $dbh = DBI->connect("DBI:mysql:myDB","username","password",) or die $DBI::errstr;

my $sth = $dbh->prepare("SELECT forcewrite FROM table WHERE primac = '$primac") || die "Error parsing SQL: $DBI::errstr\n";

$sth->execute() || die "Error executing SQL: $DBI::errstr\n";

$sth->bind_col(1, \$force  ) || die "Error binding variable: $DBI::errstr\n";

while ( $sth->fetch ) {
    print "force = $force\n";
}

# 3  
Old 11-17-2009
Code:
$
$ mysql -u test -ptest -e "select nic from t where ip = '3.3.3.3'" test
+------+
| nic  |
+------+
| CE0  |
+------+
$
$ cat -n fetchone.pl
     1  #!/usr/bin/perl -w
     2  use DBI;
     3  $dbh = DBI->connect('dbi:mysql:test','test','test');
     4  my $nic = $dbh->selectrow_array("select nic from t where ip = '3.3.3.3'");
     5  print "nic = $nic\n";
     6
$
$ perl fetchone.pl
nic = CE0
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : converting file to different scalar elements

I have a text file containing 2 exec statements as below and trying to store the below 2 execs into 2 different scalar variables in perl. /* ICD Dist, Total */ /* need to export to Excel, sheet=ICD_Dist__Total */ exec( 'select sum(count(*)) cast(count(*)*100.0/sum(count(*)) over() as... (7 Replies)
Discussion started by: scriptscript
7 Replies

2. Shell Programming and Scripting

Perl DBI error

Hi All, I installed DBI module in a non INC location and using it in my script via "use lib". But it throw the below error at the "use DBI" step. Please help Usage: DBI::_install_method(dbi_class, meth_name, file, attribs=Nullsv) at /xx/xxx/xxxxx/xxxxx/oracle/lib/DBI.pm/oracle/lib/DBI.pm line... (2 Replies)
Discussion started by: prasperl
2 Replies

3. Shell Programming and Scripting

perl: help with DBI

Hi there, I have a bit of code similar to below (which ive actually got from perldoc, but mine is similar enough) $sth = $dbh->prepare(q{ SELECT region, sales FROM sales_by_region }); $sth->execute; my ($region, $sales); # Bind Perl variables to columns: $rv =... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

4. Shell Programming and Scripting

connect to MySQL from Perl using DBI

Hi, I want to connect perl with the mysql to select and so on but the connection don't work code #!/usr/bin/perl BEGIN { # PERL MODULES WE WILL BE USING use DBI; $dbh = DBI->connect('DBI:mysql:C:\Program Files\MySQL\MySQL Server 5.0\data\db1','','pass') or die $DBI::errstr;} #... (1 Reply)
Discussion started by: eng_shimaa
1 Replies

5. Shell Programming and Scripting

populate PERL Hash

Hello I am new to perl and learning ...can you please explain why am i getting this error...as myHash is defined right above the code and I think it should be avilable in the sub function. Here is my code: <code> #!/usr/bin/perl use warnings; use strict; %myHash=(); sub... (2 Replies)
Discussion started by: uandme2k2
2 Replies

6. Shell Programming and Scripting

Installing Perl DBI and DBD

Hi, i have some queries on installing the Perl DBI and the DBD Oracle. I know that i have to install the DBI first. I have the source files in a folder in my home directory.The commands to install arecd /home/DBI Perl Makefile.PL make make installI would like to know, after executing these... (4 Replies)
Discussion started by: new2ss
4 Replies

7. Programming

perl dbi to oracle getting disconnect_all for oracle dbi help

hi i am trying to connect to an oracle database using dbi and i get this :: Driver has not implemented the disconnect_all method. at /opt/perl/lib/site_perl/5.8.0/sun4-solaris/DBI.pm line 575 END failed--call queue aborted. for all i know, the script was working earlier, but has... (1 Reply)
Discussion started by: poggendroff
1 Replies

8. Shell Programming and Scripting

scalar variable assignment in perl + { operator

When reading over some perl code in a software document, I came across an assignment statement like this $PATH = ${PROJECT}/......./.... In this particular form of scalar variable assignment, what does the curly braces operators do ? Also, what is the benefit in doing scalar assignment this... (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Shell Programming and Scripting

perl scalar variable in backquoted string

hi I've been searching all over the internet to simply do the following: $tempfile = "/usr/school/tempfile.dat"; $myvar = param('add'); ###add is the variable assigned to a popup menu `ls -l $myvar * >> $tempfile` ###I also tried `ls -l ${myvar}* >>$tempfile` open(ADDLIST,... (6 Replies)
Discussion started by: mehdi9
6 Replies

10. Shell Programming and Scripting

PERL DBI module install

We ran into an issue trying to install DBI and DB2 modules for perl for AIX from the link http://www-306.ibm.com/software/data/db2/perl/ We tried to install the DBI module using bash# perl -MCPAN -e 'install DBI' command. However we ended up with the following error. Stop. ... (3 Replies)
Discussion started by: jerardfjay
3 Replies
Login or Register to Ask a Question