A database handle is an object the represents your connection to a database. It is represented by the scalar you define it to. So as was pointed out earlier, you can create as many as you would like.
my $dbname = 'production'; my $username = 'code_monkey'; my $password = '!#@&*'; # You can also specify the database here instead of using the '$dbh->d +o("use $dbname");' my $dbh_foo = DBI->connect("dbi:Sybase:server=Foo;database=$dbname",$u +sername,$password ); # Here you can call a second one my $dbh_bar = DBI->connect("dbi:Sybase:server=Bar;database=$dbname",$u +sername,$password ); # You can even call a completely different RDBMS my $dbh_baz = DBI->connect("dbi:Pg:host=baz;dbname=$dbname",$username, +$password );
The same goes for $sth Statement Handles (which belong to their respective Database Handle). So you can create several statement handles or reuse the same one (generally with Placeholders and Bind Values). Since you don't appear to be using Placeholders and Bind Values, I will say that they are highly encouraged, as they handle quoting correctly, offer a performance increase for multiple calls, and prevent possible SQL Injection attacks.
Or reuse Statement Handles for some performance gain:my $pet = '5" Stuffed Camel'; my $sql = 'SELECT id,name FROM users WHERE has_pet = ?'; # '?' signifies a placeholder my $sth_sel = $dbh->prepare($sql); $sth_sel->execute($pet); # Now $pet is properly quoted
my $sql = 'SELECT id,name FROM users WHERE has_pet = ?'; my $sth_sel = $dbh->prepare($sql); foreach my $pet (qw/ monkey bear tiger /) { $sth_sel->execute($pet); }
I can't stress enough that you should be using Placeholders and Bind Values for slmost everything DBI related.
| XP matters not. Look at me. Judge me by my XP, do you? |
In reply to Re: database connection output
by grep
in thread database connection output
by dreman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |