Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#!/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" ;
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.
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>
- 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.