Thread: using perl DBI

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    USA
    Posts
    67
    Rep Power
    15
    hello

    i am trying to use DBI to connect to a mysql database.

    i used this

    $DSN="DBI:mysql:database";
    $db=DBI->connect($DSN,"user","pass");

    but i could not connect.

    so i used the funciton DBI->available_drivers() and found that the MySQL driver did not show up.

    is this why i could not connect? how do i get the driver onto the server? how can i get this to work?

    thanks in advance for reading my post.

    -david
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    300
    Rep Power
    15
    You go to CPAN and search for MySQL DBD DBI driver. Once you get it, simply follow the instructions inside the tarball and you'll soon get it installed. Its really no big deal...

    HTH
  4. #3
  5. No Profile Picture
    christucker
    Guest
    Devshed Newbie (0 - 499 posts)
    David,

    Go to a command line and type:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>perl -MCPAN -e shell[/code]

    This will take you into an interactive "shell" to download and install CPAN modules.
    At the command prompt you're given, type:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>install DBD::mysql[/code]

    The shell will now go out and grab all the appropriate modules and install them for you. Note, if you haven't used the CPAN shell before, you'll be asked a few configuration questions -- ignore the ones you don't understand, and fill in the others. The beauty of the CPAN shell is that it generally goes out and finds all of the dependancies you may need for a module to run, and will grab and build those too, thereby simplifying the whole process.

    One note, you may (for some modules) need a C compiler and make program. If you're on Wintel, your easiest option is MS Visual Studio, though there are various ports of the GNU tools kicking around that may work (check out cygwin.com). Naturally, on un*x systems you should have a full set of tools avaiable from GNU or your favourite proprietary vendor.

    [This message has been edited by christucker (edited July 29, 2000).]
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    USA
    Posts
    67
    Rep Power
    15
    thakns for all the information, im going to try it out. btw, how is DBD used?


    -thanks
  8. #5
  9. No Profile Picture
    christucker
    Guest
    Devshed Newbie (0 - 499 posts)
    The DBD is specified in your connect string to the database. It's completely transparent so long as you stick to 'ordinary' (i.e. relatively standardised) SQL (any of your standard SELECT, INSERT, UPDATE, etc. stuff, basically, and most ALTER, CREATE etc.). The DBD (that's DataBase Driver, by the way) is simply an interface between the abstract DBI (DataBase Interface) layer, and the database itself. So, using MySQL, all you need to know about the DBD is that its name is mysql, and so your DBI connect call will be something like:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    my $dbh = DBI->connect('DBI:mysql:mydatabase', $user, $pass, $extra_options);
    [/code]
    Most of the time $extra_options won't be used -- if you do need it, it's just a reference to a hash of options like RaiseError. There's loads of info on that in the DBI PODs.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    300
    Rep Power
    15
    That's what I like about it. If you want to move your script from MySQL to, say, ODBC, you just have to change that 1 line of code. Most of the time, anyway...

IMN logo majestic logo threadwatch logo seochat tools logo