Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am trying to create a table in CreateTable sub and update the created table using load data infile in UpdateTable sub. dbh = DBI->connect statement worked well in the first sub and I have the table created. Next sub,it raised an error: DBD::mysql::db do failed: Access denied for user 'bla'@'localhost' (using password: YES) at names_db.pl line 60. I have disconnected the earlier dbh. How do I connect to the same db again?Do I really need to do that?

Replies are listed 'Best First'.
Re: DBI connect problems
by psini (Deacon) on Jun 12, 2008 at 15:18 UTC

    If I understand what you are asking:

    • No, you don't need to open a db connection every time you do a query: you could better create the db connection as a global var and use it in the subs.
    • Yes, it should reconnect. If it does not there is probably something wrong in your code or in your db server config.

    If you want a more significant answer you should post at least an abstract of your code.

    Careful with that hash Eugene.

      Here is the code:Its working well on the windows.I am trying to run this on linux.
      #!/usr/local/bin/perl use DBI; &MakeTable; &InsertValues; sub MakeTable() { use DBI; #print "enter username\n"; $dbh = DBI->connect("dbi:mysql:namesdb", 'some' , 'someother',{ Auto +Commit => 1, RaiseError => 1}) or die "connection error:$DBI::errstr\ +n"; $dbh-> do( "create table if not exists names ( #stuff );" ); $dbh-> disconnect; }#sub ends here; sub InsertValues() { $tablename="names"; $infile = '/path/names.dmp'; # $field= "\t|\t"; # field ending syntax from file; $lines="\t|\n";#lines ending syntax from file; $command = qq ~load data infile '$infile' ignore into table $tablen +ame FIELDS terminated by '$field' LINES terminated by '$lines';~; $dbh = DBI->connect("dbi:mysql:namesdb", 'some' , 'someother',{ Auto +Commit => 1, RaiseError => 1}) or die "connection error:$DBI::errstr\ +n"; $dbh->do($command); $dbh-> disconnect; }

      Jun 14, 2008 at 04:22 UTC McDarren Added code tags, removed excessive whitespace

        Please, next time use <code> tags to embed code in your posts, I had to reformat all the code to be able to read it.

        I see that you use DBI inside the first sub, and this is unnecessary for you have already used it before.

        I see also that you don't use strictures and this is bad.

        use strict; use warnings;

        are your friends.

        I don't know why the second call returns an error, as I said before best practice is to connect to db once at the beginning and disconnect at the end of the script. If you want to connect/disconnect in every sub, try adding strictures, add the global part of your code (the calls to the subs), execute it and see which error(s) results

        After this, post again the "complete" code (the minimal code that can be executed giving the error) AND the resulting error (sorry, I can't test it because I don't have mysql installed on my machine)

        Careful with that hash Eugene.

Re: DBI connect problems
by olus (Curate) on Jun 12, 2008 at 15:17 UTC

    You'll have to configure mysql to accept connections from user 'bla' and the locations it can connect from. You do that on the 'mysql' database, 'users' table.

      Hi I can connect from any location-Desktop or from some other folder. the error that i am getting is: Access denied for user 'bla'@'localhost' (using password: YES) at names_db.pl line 50. when I am doing manipulations for second time;
Re: DBI connect problems
by Anonymous Monk on Jun 12, 2008 at 15:12 UTC
    I am sorry I forgot add something, the code worked great on Windows,I am currently trying to run that on Linux -ubuntu.