LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2013, 04:01 AM   #1
zak100
Member
 
Registered: Jul 2009
Posts: 262

Rep: Reputation: 2
connecting mysql using perl


Hi,
I am trying to connect with mysql data base using perl but i am getting DBI error problem. I have search the web but i am not able to find any solution.
I am able to display the table using mysql
Code:
[guest@HP-UX608YY ~]$ su
Password: 
[root@HP-UX608YY guest]# service mysqld start
Starting MySQL:                                            [  OK  ]
[root@HP-UX608YY guest]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.67 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.




mysql> use perltest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed



mysql> select * from samples;
+----+------+----------+
| id | name | phone    |
+----+------+----------+
|  1 | abc  | 555-5555 | 
|  2 | xyz  | 555-6666 | 
+----+------+----------+
2 rows in set (0.02 sec)

mysql>
My perl code is :

Code:
guest@HP-UX608YY zulfi]$ cat p1.pl
#!/usr/bin/perl
use DBI;
$dbh = DBI->connect('dbi-mysql:perltest:samples','root','')
or die "connection error DBI::errstr\n";
$sql ="select * from samples";
I am getting following error:
Code:
[guest@HP-UX608YY zulfi]$ perl p1.pl
Can't connect to data source 'dbi-mysql:perltest:samples' because I can't work out what driver to use (it doesn't seem to contain a 'dbi:driver:' prefix and the DBI_DRIVER env var is not set) at p1.pl line 3
Somebody plz help me.

Zulfi.

Last edited by zak100; 09-26-2013 at 04:04 AM.
 
Old 09-26-2013, 04:17 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Try this:
Code:
#!/usr/bin/perl
use strict ;
use warnings ;

my $dbh ;
my $sql ;

use DBI ;

$dbh = DBI->connect( 'DBI:mysql:perltest', 'root', 'pwd_goes_here' ) || die "Could not connect to database: $DBI::errstr" ;

$sql = "select * from samples" ;
Perl DBI examples for DBD::mysql
 
1 members found this post helpful.
Old 09-26-2013, 11:36 PM   #3
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Hi,
Thanks for your reply. It helped me but now i am getting some other errors while displaying the contents of db. My code is now:
Code:
#!/usr/bin/perl
use strict ;
use warnings ;

my $dbh ;
my $sql ;

use DBI ;

$dbh = DBI->connect( 'DBI:mysql:perltest', 'root', '' ) || die "Could not connect to database: $DBI::errstr" ;

$sql = "select * from samples" ;
$sth = $dbh->prepare($sql);
$sth->execute or die "SQL error $DBI::errstr\n";
while (@row = $sth->fetchrow_array)
{
print "@row\n";}

~
The errors are:

Code:
[guest@HP-UX646UU zulfi]$ cat p3.pl
#!/usr/bin/perl
use strict ;
use warnings ;

my $dbh ;
my $sql ;

use DBI ;

$dbh = DBI->connect( 'DBI:mysql:perltest', 'root', '' ) || die "Could not connect to database: $DBI::errstr" ;

$sql = "select * from samples" ;
$sth = $dbh->prepare($sql);
$sth->execute or die "SQL error $DBI::errstr\n";
while (@row = $sth->fetchrow_array)
{
print "@row\n";}

[guest@HP-UX646UU zulfi]$ perl p3.pl
Possible unintended interpolation of @row in string at p3.pl line 17.
Global symbol "$sth" requires explicit package name at p3.pl line 13.
Global symbol "$sth" requires explicit package name at p3.pl line 14.
Global symbol "@row" requires explicit package name at p3.pl line 15.
Global symbol "$sth" requires explicit package name at p3.pl line 15.
Global symbol "@row" requires explicit package name at p3.pl line 17.
Execution of p3.pl aborted due to compilation errors.
[guest@HP-UX646UU zulfi]$
Plz guide me. I executed this sort of program on these computers on year ago but now its not working.

Zulfi.
 
Old 09-27-2013, 12:17 AM   #4
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Hi,
I have used the show tables commands and its also showing me the correct table. Plz guide how to solve this error.


Code:
mysql> use perltest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables
    -> ;
+--------------------+
| Tables_in_perltest |
+--------------------+
| items              | 
| samples            | 
| samples2           | 
| subcribers         | 
+--------------------+
4 rows in set (0.12 sec)

mysql>
Zulfi.
 
Old 09-27-2013, 12:53 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,837

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you can try to insert the following line into that p3.pl (line 7):
my @row, $sth;
 
Old 09-27-2013, 12:53 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You'll find these useful, but note to start with that 'use strict;' (highly recommended) requires you to declare vars before use eg 'my $var;'

http://www.perlmonks.org/?node=Tutorials
http://www.perlmonks.org/?node_id=284436
http://www.tizag.com/perlT/index.php
 
Old 09-27-2013, 01:39 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@zak100: Here's an article that explains why you should use use strict and use warnings (and possibly use diagnostics):
- Use strict warnings and diagnostics or die

In short:

- use warnings ; # turns on all sorts of warnings about probable errors.
- use strict ; # generates compile and run-time errors for certain unsafe constructs.

The above two should always be used when programming in perl.

This one might come in handy when you want more details. Not always needed but a good one to know.
- use diagnostics ; # optional; causes warnings to be explained in greater detail.
 
Old 09-28-2013, 10:25 AM   #8
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Hi,
Thanks fellows. I am able to catch my error. I was using a '-' instead of colon. Now its working.

Code:
$dbh = DBI->connect('dbi-mysql:perltest:samples','root','')
Thanks for your support in this problem.

Zulfi.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing Perl (v5.8.8) module for connecting to MySQL Abhinav Kakkar Red Hat 1 08-02-2010 09:27 AM
Bugzilla - MySQL - perl - DBD::mysql install problem amchargue Linux - Software 3 09-10-2008 12:01 AM
Bugzilla - MySQL - perl - DBD::mysql install problem Runningonair Linux - Software 8 10-12-2007 12:42 AM
Error compiling perl-DBD-MySQL after upgrading to MySQL 4.1 pitaro920 Linux - Software 1 01-16-2006 04:28 PM
Mysql is not connecting from servlet.But connecting from java, help pls Harish_f Linux - General 0 05-08-2002 03:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration