Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Mysql and Perl Module

by endymion (Acolyte)
on Jun 14, 2012 at 08:41 UTC ( #976144=note: print w/ replies, xml ) Need Help??


in reply to Re: Mysql and Perl Module
in thread Mysql and Perl Module

Hello Tospo, the problem is it should be an module we can use for all of our customer scripts. The point is, that we need a module where I can only give a few arguments to it. Is it better to make another sub for the connection ?


Comment on Re^2: Mysql and Perl Module
Re^3: Mysql and Perl Module
by Anonymous Monk on Jun 14, 2012 at 09:26 UTC

    See this example program Re: Open multiple file handles?

    You can easily create a module with a function like this

    sub IntoDbImportCSV { my( $dbiconn, # dbi connection string $colnames, # column names $csvfile, # path to csv file $csvargs, # Text::CSV->new options hash ) = @_; my $dbh = DBI->connect( $dbiconn, undef, undef, { RaiseError => 1, PrintError => 1, }, ); open my($infh), '<', $csvfile ... my $csv_in = ... $dbh->begin_work; my $sth = $dbh->prepare($sql); while ( my $row = $csv_in->getline( $infh ) ) { $sth->execute( @{ $row } ); } $dbh->commit; $dbh->disconnect; } IntoDbImportCSV( 'dbi:SQLite:dbname=temp.test.sqlite', '/path/to/foo.cvs', ['ro', 'sham', 'bo' ], { quote_char => '"', sep_char => ',', allow_loose_escapes => 1, empty_is_undef => 1, binary => 1, auto_diag => 1, }, );
Re^3: Mysql and Perl Module
by tospo (Hermit) on Jun 14, 2012 at 09:29 UTC
    The ideal situation for that scenario is that your module takes some sort of config file with the DSN for the databse connection so that you can ship it with a tailor-made config file for each of your customers.
    Look at something like Config::General to handle such a config.
    Alternatively, you could make the DSN an argument that needs to be supplied by the user when running this script.
    If you have a lot of stuff to do with that database then it would also be worth exploring something like DBIx::Class to handle your database in a much more abstract way.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://976144]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2014-11-27 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (180 votes), past polls