I used to use Perl with flat database files because I thought it was the simplest and best way to go. Then I discovered mySQL--this reads like a laundry detergent commercial. Anyway, mySQL has worked out much better for me. It does take a bit of an effort to get started, but it's not too bad with the proper books and some patience. As for its performance compared to other databases, it ranks as high as Oracle these days, according to
e-week.
Regarding your concerns about exporting to delimited text files, that's a breeze with mySQL and Perl and the Perl DBI module. As with everything else with Perl, there are many ways of doing it, but below is a simple overview of an export program. This export program is assuming that you've already set up a database and table in mySQL, of course.
#!/usr/bin/perl -w
use DBI;
# Connect to the mySQL server and
# get today's orders.
my $dbh = DBI->connect("DBI:mysql:orders:localhost","user","passwd")
|| die "Could not connect:".DBI->errstr;
my $sql_stmnt = "SELECT order_id, item_no, qty, price
FROM orders WHERE order_date=CURRENT_DATE'";
my $sth = $dbh->prepare($sql_stmnt);
$sth->execute();
while(@orders = $sth->fetchrow_array()) {
$order_id = $orders[0];
$item_no = $orders[1];
$qty = $orders[2];
$price = $orders[3];
# Create record with bars as delimiters
# and push record into array for exporting.
$order = $order_id . "|" . $item_no . "|" . $qty . "|" . $price . "
+\n";
push(@export, $order);
}
$sth->finish();
$dbh->disconnect();
# Now loop through array of orders
# and save to text file.
open(EXP, ">client-orders.txt")
|| die "Could not open export file: $!";
foreach(@export){ print EXP; }
close(EXP);
This bit of code is a lot wordier than it needs to be and can be consolidated into few lines. However, when starting out I think its better to spread the code out to keep your sanity and to make learning and trouble-shooting easier.
-Spenser
That's Spenser, with an "s" like the detective.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.