Hi Denny,

>How can i use Perl DBI to create tables in mysql?

I am not an expert in CPAN modules for Perl but I am using the DBD module
that needs DBI module for access the MySQL. Please see the man pages for DBI
module.

I am using the:
   DBI-1.22
   DBD-mysql-2.1017

You can installing all mentioned and all dependant modules with

  perl -MCPAN -e 'install Bundle::DBD::mysql'

Before rushing execute the perl command mentioned above you need to have
compiled MySQL client library and source files (especially header files)
otherwise you are not able to install the DBD:mysql module on your platform.
Please read INSTALL.htm file that is delivered with the DBD::mysql module.

After successful instalation of the DBD::mysql module I have used example
from DBD::mysql module documentation.

Please see the mentioned EXAMPLE below:
         #!/usr/bin/perl

         use strict;
         use DBI();

         # Connect to the database.
         my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost",
                                "joe", "joe's password",
                                {'RaiseError' => 1});

         # Drop table 'foo'. This may fail, if 'foo' doesn't exist.
         # Thus we put an eval around it.
         eval { $dbh->do("DROP TABLE foo") };
         print "Dropping foo failed: $@\n" if $@;

         # Create a new table 'foo'. This must not fail, thus we don't
         # catch errors.
         $dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");

         # INSERT some data into 'foo'. We are using $dbh->quote() for
         # quoting the name.
         $dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")");

         # Same thing, but using placeholders
         $dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");

         # Now retrieve data from the table.
         my $sth = $dbh->prepare("SELECT * FROM foo");
         $sth->execute();
         while (my $ref = $sth->fetchrow_hashref()) {
           print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
         }
         $sth->finish();

         # Disconnect from the database.
         $dbh->disconnect();


I hope that it helps.

Viliam Batka

----- Original Message -----
From: "Denny" <[EMAIL PROTECTED]>
To: "Mysql maillist" <[EMAIL PROTECTED]>
Sent: Thursday, May 23, 2002 8:02 PM
Subject: Perl DBI


> Hi,
>
> How can i use Perl DBI to create tables in mysql?
>
> Denny
>
> __________________________________________________
> Do You Yahoo!?
> Everything you'll ever need on one web page
> from News and Sport to Email and Music Charts
> http://uk.my.yahoo.com
>
> ---------------------------------------------------------------------
> Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
>
> To request this thread, e-mail <[EMAIL PROTECTED]>
> To unsubscribe, e-mail
<[EMAIL PROTECTED]>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to