Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hidin database connection parameters

Status
Not open for further replies.

JackTheRussel

Programmer
Joined
Aug 22, 2006
Messages
110
Location
FI
Hi!

I have application where my connection parameters are written into code
Code:
my $database_host     = 'localhost';
my $database_name     = 'MyDatabase';
my $database_username = 'root';
my $database_password = 'Password';
my $dsn               = 'DBI:mysql:'.$db_name.':'.$db_host;

Now I would like to hide my connection parameters into connection.ini file and I have try this kind of solution, but it dosent work:

connection.ini
Code:
[DataBaseParameters]

database_host     = localhost
database_name     = MyDatabase
database_username = root
database_password = Password
dsn         = DBI:mysql

And I have try to connect into database like this:
Code:
my $database_connection = DBI->connect($database_options{"dsn"}$database_options{"database_username"},
$database_options{"database_password"},$database_options{"database_localhost"},$database_options{"database_name"});

And I have write references to the connection.ini file, but
I get error message: Can't connect to data source , no database driver specified and DBI_DSN env var not set at xxx/xxx/xxx

DBI::connect('DBI', 'undef', 'undef', 'undef') called at /xxxx/xxx/xx...

Could some one help me?
 
Thanks Tony, but that didnt solve the problem.

I still get same error ?
 
The connection command I always use is:
[tt]
DBI->connect("DBI:mysql:$dbname:$hostname",$username,$password);
[/tt]
 
Yes I use it as well and it works fine, but now I would like to get these variables: $dbname, $username etc... from connection.ini file

So then I dont have to write those variables straigh to the code.
 
Then just substitute those variables with the $database_options{...} equivalents.
 
I may be missing something obvious, but I don't see the part where you read the *.ini file and parse it into %database_options? Are you using a module to do this?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Ok. But if I try like this:

Code:
#my $db_co = DBI->connect("DBI:mysql:$database_options{'db_name'}:$database_options{'db_host'}",$database_options{'db_username'},$database_options{'db_password'});

I'll still get error:

DBD::mysql::st execute failed: No database selected at /home/xxx/etc..

 
Try using
Code:
use Data::Dumper;
.
.
.
print Dumper(%database_options);
does it look like what you are expecting? In other words, is the problem with the database connect call, or the contents of the variables you are passing to it? Perhaps your parsing isn't doing quite what you expect...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
I finally solved this problem.

Right statement was:

Code:
my $db_co = DBI->connect("DBI:mysql:$database_option{'Section'}{'Parameter'}:$database_option{'Section'}{'Parameter'}",$database_option{'Section'}{'Parameter'},$database_option{'Section'}{'Parameter'}) ;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top