Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Lost over DBI in perl

by sdyates (Scribe)
on Aug 22, 2011 at 00:27 UTC ( [id://921563]=perlquestion: print w/replies, xml ) Need Help??

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

UPDATE: DBD::mysql will not install on mac or windows... this is my problem. Anyone have instructions on how to set up DBD::myssql on a mac (lion)? Folks, its been over 6 years since I coded perl and am having trouble with something so simple but cannot recall what I need to do a would appreciate someone to point me in the right direction. Please use simple terms too as my technical vocal has suffered :P
DBI connect('d60380940:192.168.1.3','HASH(0x238b3c)',...) failed: [Mic +rosoft][ODBC Driver Manager] Data source name not found and no defaul +t driver specified (SQL-IM002) at CRTDBTBL.pl line 46 Cannot connect to the database ERROR: 1 ([Microsoft][ODBC Driver Manager] Data source name not foun +d and no d efault driver specified (SQL-IM002))
I have the DB installed on windows, using 5.12. The password and users name I am using is root and the db and ip are correct - I use the same info on a mac client to connect to the db no problem (mac app, not command line). I am not even sure what data source name means. Sorry for the bad post, but I need to get something accomplished tonight and have already spent hours online trying to solve it. Thanks for your time, S
sub open_dbi { # Declare and initialize variables my $host = '192.168.1.3'; my $db = d60380940; my $db_user = root; my $db_password = root; # Connect to the requested server # my $dbh = DBI->connect("dbi:mysql:$db:$host", "$db_user", "$db_pa +ssword", {RaiseError => 0, PrintError => 0} ) or err_trap("Cannot con +nect to the database"); my $dbh = DBI->connect("dbi:ODBC:d60380940:192.168.1.3", {RaiseErr +or => 0, PrintError => 0} ) or err_trap("Cannot connect to the databa +se"); return $dbh; }

Replies are listed 'Best First'.
Re: Lost over DBI in perl
by Tux (Canon) on Aug 22, 2011 at 06:35 UTC

    Just some side-commands.

    kejohm didn't say so, but he also dropped the double quotes around single scalar variables. They here only function as forced stringification of the two variables for user/password, wich disables undef (which is valid). I did include hist quotation fixes below. The optional arguments after the driver prefix (dbi:ODBC:) are seperated by semi-colons, not colons. User and password are not optional.

    The DSN for ODBC is kinda special, please use 'perldoc DBD::ODBC::FAQ' to read all about it.

    sub open_dbi { # Declare and initialize variables my $host = "192.168.1.3"; my $db = "d60380940"; # NEEDS QUOTES my $db_user = "root"; # NEEDS QUOTES my $db_password = "root"; # NEEDS QUOTES # Connect to the requested server # MySQL example my $dbh = DBI->connect ("dbi:mysql:$db:$host", $db_user, $db_passw +ord, { RaiseError => 1, # When expecting problems, EN +ABLE! PrintError => 1, # When expecting problems, EN +ABLE! ShowErrorStatement => 1, # When expecting problems, EN +ABLE! FetchHashKeyName => "NAME_lc", # Always be predictable in ke +y names }) or err_trap ("Cannot connect to the database"); # ODBC example semi-colon, not colon ---. ,------- Needs + Host= prefix my $dbh = DBI->connect ("dbi:ODBC:DSN=d60380940;Host=192.168.1.3", + undef, undef, { RaiseError => 1, # When expecting problems, EN +ABLE! PrintError => 1, # When expecting problems, EN +ABLE! ShowErrorStatement => 1, # When expecting problems, EN +ABLE! FetchHashKeyName => "NAME_lc", # Always be predictable in ke +y names }) or err_trap ("Cannot connect to the database"); return $dbh; }

    Enjoy, Have FUN! H.Merijn
Re: Lost over DBI in perl
by kejohm (Hermit) on Aug 22, 2011 at 02:04 UTC

    First off, you want to be running under strict and warnings. This will catch any errors that you may have in your strict, so put this at the top of your code:

    use strict; use warnings;

    (You could also usediagnostics for more verbose messages.)

    This means that for your sample, you will need to quote the database name, user name and password, so Perl won't complain when it finds an unknown bareword.

    In order to specify a particular host to connect to, use the 'host' parameter in the data source, eg:

    sub open_dbi { # Declare and initialize variables my $host = '192.168.1.3'; my $db = 'd60380940'; my $db_user = 'root'; my $db_password = 'root'; # Connect to the requested server # my $dbh = DBI->connect("dbi:mysql:database=$db;host=$host", $db_u +ser, $db_password, {RaiseError => 0, PrintError => 0} ) or err_trap(" +Cannot connect to the database"); my $dbh = DBI->connect("dbi:ODBC:database=$db;host=$host", $db_use +r, $db_password, {RaiseError => 0, PrintError => 0} ) or err_trap("Ca +nnot connect to the database"); return $dbh; }
Re: Lost over DBI in perl
by kcott (Archbishop) on Aug 22, 2011 at 06:59 UTC

    sdyates, welcome back to Perl after six years.

    As already pointed out by kejohm, the first thing to do is add strictures and warnings: this will highlight the 3 barewords you have in the code fragment shown and similar problems in the rest of your code.

    The next step is to check the documentation for the DBD (database driver) for the database you're using:

    • DBD::mysql - provides the format for the DBI->connect() call.
    • DBD::ODBC - provides a template for DBI->connect() and links to DBD::ODBC::FAQ which has details (including notes on the DSN).

    You'll find similar modules and documentation for other databases in the DBD:: space (should you need them).

    -- Ken

Re: Lost over DBI in perl
by jethro (Monsignor) on Aug 22, 2011 at 02:00 UTC

    I don't know anything about ODBC, so this may be way off. But shouldn't there be a user and password in your call to DBI->connect? You only provide them to the call that is commented out

Re: Lost over DBI in perl
by Neighbour (Friar) on Aug 22, 2011 at 11:07 UTC
    The error you pasted is caused by the 2nd connect statement (the one using ODBC). It would appear the first one (using dbi:mysql) just works.
    Have you checked if $dbh is undef after the first DBI->connect call?

    Edit: My bad. The first call was commented :P
    The error you're getting now (Datasource not found) is because ODBC requires you to register database-connections with the system.
    The connectionstring for ODBC is this:

    $dbh = DBI->connect("dbi:ODBC:$DSN", $username, $password, $hr_options +) or die $DBI::errstr;
    $DSN is the name of the datasource you registered with the system. Under *nix, this is /etc/odbc.ini. I have no idea what file is used in lion.
      Thanks for the several error you all pointed out. Many of those were made trying to figure out what was networking. The real cause was that the DBD::mysql was not installing properly. Thanks all!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://921563]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found