CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: DBI:
Connecting MYSQL Database using Perl

 



Murali
New User

Feb 26, 2008, 1:38 AM

Post #1 of 6 (6825 views)
Connecting MYSQL Database using Perl Can't Post

Hi,

I have installed Perl 5 to my Windows, But when try to connect the mysql database using the perl script, it is throwing some error.

Here is my code

Use Mysql;

## connect to mySQL database:
$DBHOST = "localhost";
$DBNAME = "mydatabase";
$DBUSER = "root";
$DBPASS = "passwd";

$DB = Mysql->connect($DBHOST, $DBNAME, $DBUSER, $DBPASS);

## Query to the database:

$SQuery = qq~CREATE TABLE member(member_id INTEGER NOT NULL,member_name VARCHAR(100),member_status TINYINT DEFAULT 1 NOT NULL)~;


Error:

E:\Perl>perl dbtest.pl
Can't locate Mysql.pm in @INC (@INC contains: E:/Perl/lib E:/Perl/site/lib .) at
dbtest.pl line 2.
BEGIN failed--compilation aborted at dbtest.pl line 2.

Can some one clarify this?.

Whether I need to add MYSQL Connectors to any specified path of the Perl Installed directory?

Some one provide me a simple example for executing a simple query using perl with mysql on windows
-Murali

(This post was edited by Murali on Feb 26, 2008, 1:42 AM)


rajiv_chennai
Novice

Feb 26, 2008, 5:07 AM

Post #2 of 6 (6817 views)
Re: [Murali] Connecting MYSQL Database using Perl [In reply to] Can't Post

have u install that module.If you have any doubt on this module just refer in CPANhttp://search.cpan.org/~rudy/DBD-mysql-2.9008/lib/Mysql.pm

Regards
Rajiv


Murali
New User

Feb 27, 2008, 3:33 AM

Post #3 of 6 (6801 views)
Re: [rajiv_chennai] Connecting MYSQL Database using Perl [In reply to] Can't Post

Thanks Rajiv.

It really worked, i have installed the DBI and DBD::MYSQL Lib using the following command

ppm install DBI
ppm install DBD::mysql


But when i execute the file, then it seems executed successfully

E:\Perl>perl dbtest.pl

E:\Perl>

But could not find the table whch i used to create in the executed script.


E:\Perl>mysql -u root -p
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.0.50sp1a-enterprise-gpl-nt MySQL Enterprise Server (GPL)

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

mysql> show databases;


Code
+--------------------+ 
| Database |
+--------------------+
| information_schema |
| murali |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)



mysql> use murali
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql>




Quote


$SQuery = qq~CREATE TABLE member(member_id INTEGER NOT NULL,member_name VARCHAR(100),member_status TINYINT DEFAULT 1 NOT NULL)~;



is that enough or else i need to specify some command for executing the query?

Can u please provide me a simple example for connecting the database and executing a query?
-Murali


rajiv_chennai
Novice

Feb 27, 2008, 9:53 PM

Post #4 of 6 (6789 views)
Re: [Murali] Connecting MYSQL Database using Perl [In reply to] Can't Post


Code
use Mysql; 

## connect to mySQL database:
$DBHOST = "localhost";
$DBNAME = "mydatabase";
$DBUSER = "root";
$DBPASS = "passwd";

$DB = Mysql->connect($DBHOST, $DBNAME, $DBUSER, $DBPASS);

## Query to the database:

$SQuery = qq~CREATE TABLE member(member_id INTEGER NOT NULL,member_name VARCHAR(100),member_status TINYINT DEFAULT 1 NOT NULL)~;

$sth = $DB->query($SQuery);


You should execute the query after query construction.The above code should work fine. Read the Mysql.pm Manual in CPAN.

Hope Its Help

Regards
Rajiv


KevinR
Veteran


Feb 27, 2008, 10:59 PM

Post #5 of 6 (6786 views)
Re: [rajiv_chennai] Connecting MYSQL Database using Perl [In reply to] Can't Post

While I am not good with the SQL stuff I know that the MySQL module is considered obsolete and DBI and DBD::Mysql should be used instead.
-------------------------------------------------


Murali
New User

Feb 28, 2008, 6:35 AM

Post #6 of 6 (6778 views)
Re: [KevinR] Connecting MYSQL Database using Perl [In reply to] Can't Post

Thanks Rajiv, Worked Fine.
-Murali

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives