By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

how to connect in database

P: 10
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...
Sep 19 '08 #1
Share this Question
Share on Google+
2 Replies


P: 2
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use DBI;
  3.  
  4. my $db = DBI->connect( 'databasename',
  5.                         'username',
  6.                         'usepassword',
  7. $db->disconnect();
  8.  
Sep 19 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
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:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use DBI:
  4. use DBD::mysql;
  5.  
  6. my $Host = "localhost";
  7. my $DbName = "dbname";
  8. my $DbUser = "username";
  9. my $DbPassword = "xxxxx"; # personally, I would pull this from a config file.
  10.  
  11. $dbh = DBI->connect("dbi:mysql:host=$Host;database=$DbName", $DbUser , $DbPassword) or die;
  12.  
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
Sep 19 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.