On 22/05/2013 06:55, Gabor Szabo wrote:
Hi,
recently I published an introduction to DBI article on the Perl Maven site:
http://perlmaven.com/simple-database-access-using-perl-dbi-and-sql
the really interesting thing is that articles are constantly being
translated to 13 languages.
The Korean version has already arrived:
http://ko.perlmaven.com/simple-database-access-using-perl-dbi-and-sql
regards
Gabor
Gabor,
Another good tutorial - please keep up the good work of promoting Perl.
I have a few comments I hope you'll take constructively. Please ignore
some of these if you think it is beyond the scope of your tutorial - it
is difficult for me to tell the audience you are targeting.
"Those drivers are compiled together with the C client libraries of the
respective database engines." although this is mostly true there are
pure perl DBDs.
"The DSN (Data Source Name) (in the $dsn variable) is very straight
forward. It contains the type of the database. That will be the clue to
DBI which DBD to load. In case of SQLite, the only thing we really need
is the path to the database file."
It may be a language thing but it is hardly a "clue". DBI clearly states
the connection string is dbi:DRIVER_NAME:something_else and DRIVER_NAMEs
are registered with DBI. Its also not always as "straight forward" as
you suggest as the something_else is usually a ';' separated string of
attributes and values e.g. "DRIVER={this driver}".
"The call to disconnect from the database is optional as it will
automatically be called when the variable $dbh goes out of scope, but it
having it might be a clear indication for the next programmer dealing
with this code, that you are done with the database. "
There are a number of gotchas with this. You may still have a select
statement active where you've not fetched all the rows yet from the
cursor in which case you'll get a warning. Also, you may be in the
middle of a transaction and in that case the transaction may be rolled back.
"INSERT"
You've presented an insert with an 4 columns and then inserted 3 - I
know you know why but that might confuse people i.e., it is an auto
incrementing column with a default value. SQLite is a bit different from
other database in this respect as most would require you to define the
id column as auto incrementing or having a default value.
"UPDATE"
Your example with the do method if fine but often people want to insert
or update multiple rows and I think it is worth showing you can prepare
a stmt and execute it many times with different parameters. As you do
with the select example.
"This is by far the most interesting part of the database access. As the
SELECT statement can return a lot of rows and a lot of values in each
row we cannot use a simple call to the do method. "
Some DBDs really dislike using the do method for select stmts and
especially multiple stmts in the same SQL e.g., do(q/something; select
something from something/);
Martin
--
Martin J. Evans
Wetherby, UK