how to connect in database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fatimang
    New Member
    • Sep 2008
    • 10

    how to connect in database

    hi......can you please help me to connect in database using perl...what is the appropriate database to use and what is the connection string.....
    thank you...
  • erpritish
    New Member
    • Sep 2008
    • 2

    #2
    Code:
    use strict;
    use DBI;
    
    my $db = DBI->connect( 'databasename',
                            'username',
                            'usepassword',
    $db->disconnect();
    Last edited by numberwhun; Sep 19 '08, 12:02 PM. Reason: Please use code tags

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      erpritish: The way that you have written that code it would fail. The format is incorrect and it includes an out of place disconnect all within the same statement. Also, when you post code in the forums, you really need to use the necessary code tags. There is an example of them to the right of the Reply window in the "Reply Guidelines" box. Please use them next time. Also, in these forums, we don't just provide code that is requested as this is not a scripting service. While I know you are being helpful, it does not help the OP learn.

      fatimang: You are really going to want to take a look at the DBI module page on CPAN . It will tell you all that you need to know about connecting to a database. In the mean time try this:

      Code:
      use strict;
      use warnings;
      use DBI:
      use DBD::mysql;
      
      my $Host = "localhost";
      my $DbName = "dbname";
      my $DbUser = "username";
      my $DbPassword = "xxxxx"; # personally, I would pull this from a config file.
      
      $dbh = DBI->connect("dbi:mysql:host=$Host;database=$DbName", $DbUser , $DbPassword) or die;
      That will at least get you connected to the database, but what you do from there is up to you. I know that I said earlier in this post that I don't normally just give out code, as it won't help you learn, but since the other OP provided you what they did, I posted this to give you a better example. Next time you will probably be asked to show the code you have tried and any errors you see as well.

      Regards,

      Jeff

      Comment

      Working...