connect to mysql database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maverickx
    New Member
    • Jun 2007
    • 10

    connect to mysql database

    Hi everyone,
    i am a totally rookie in perl. I have a project which needs to use perl to connect to mysql database. I already installed the perl mysql driver, MySQL, and use perl code which i found on the internet to connect to the mysql database. However, it says the connection was denied. I didnt set username and password for the database.

    So which problems might result in this situation, need help please
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    What code have you tried so far to connect to a database?

    - Miller

    Comment

    • maverickx
      New Member
      • Jun 2007
      • 10

      #3
      Originally posted by miller
      What code have you tried so far to connect to a database?

      - Miller
      hi miller,
      here is the code

      Code:
      #!/usr/bin/perl -w
      #connect_test.pl
      
      use strict;
      use DBI;
      
      print "Available Database Drivers:\n";
      print "*" x 40, "\n";
      print join("\n", DBI->available_drivers()), "\n\n";
      
      
      my %attr => ( RaiseError => 0 );
      
      my $dbh = DBI->connect("DBI:mysql:dbname=Orkut_data:localhost", "root", "root")
      	or die("Error: $DBI::errstr");

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Obviously the first place that you should start is the documentation.
        cpan DBI#connect

        Secondly, whenever I create a database connection, I always extrapolate the settings into variables. This is a way of self-documenting, so that you can be assured of what goes where.

        [CODE=perl]
        my $dbname = 'Orkut_data';
        my $dbhost = 'localhost';
        my $dbuser = 'root';
        my $dbpass = 'root';

        my $dbh = DBI->connect("DBI:m ysql:dbname=$db name:$dbhost", $dbuser, $dbpass)
        or die "Error: $DBI::errstr";
        [/CODE]

        You claimed that you set no user or pass, but as you can see you are trying to connect to the database with the u/p root/root. Is this what you want?

        Also, whenever a connection fails, the DBI::errstr should have given you a useful error message. If you continue to have problems, then tell us what those are if you can't translate their meaning.

        - Miller

        Comment

        • maverickx
          New Member
          • Jun 2007
          • 10

          #5
          i am using EMS SQL Manager to create a database.

          It does not work. it says the access was denied.
          i also installed MySQL Server 5.0/

          so why i cant create a database?

          Comment

          • savanm
            New Member
            • Oct 2006
            • 85

            #6
            Hi

            use strict;
            use DBI;
            #connect to the database
            my $dbh = DBI->connect("DBI:m ysql:database=p erldbi;host=loc alhost",
            "root","");
            #$dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");
            #$dbh->do("DROP TABLE foo");
            my $sth = $dbh->prepare("SELEC T * FROM foo");
            $sth->execute();
            while (my $ref = $sth->fetchrow_hashr ef()) {
            print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
            }
            $sth->finish();
            $dbh->disconnect() ;

            Comment

            Working...