digger has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that writes form data to a simple DBD:CSV file. I have tried a number of ways to write to this, and I am stumped. As you can see in the code, I have tried creating the file on the fly if it does not exist. I also tried creating a blank file using touch and then chmod 666 or 777.
I get no error messages, and my confirmation page shows the correct data. In the first case, the file is never created, and in the second, the file remains blank.

The company I host with provides cgiwrap, and I tried using that when running my script, with no luck, so it doesn't look like a permissions issue. I am using Carp(fatalsToBrowser), so if something is dreadfully wrong here, wouldn't it be reported back to me? Am I missing something obvious?
my $dir='../admin/'; my $table='EVENTS'; my $dbh= DBI->connect("DBI:CSV:f_dir=$dir")or die "Cannot connect: " . + $DBI::errstr; #if the table does not exist, make it #since we are using DBD:CSV, I am not sure that the field type/size de +clarations #are necessary, or even applied. Including them will make the code mor +e portable if #we move to mysql in the future. if (! -e "$dir$table") { print "making table $dir$table"; my $sth= $dbh->prepare ("CREATE TABLE $table (ukey CHAR(20), city CHAR(45), date CHAR(10), time CHAR(8), title CHAR(64), location CHAR(250), contact CHAR(64), email CHAR(60), phone CHAR(14) type CHAR(1))") or die "Cannot prepare: " . $dbh->errstr( +); $sth->execute() or die "Cannot execute: " . $sth->errstr(); } my $sth=$dbh->prepare(qq{INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?, +?)}) or die "Cannot prepare: " . $dbh->errstr(); $sth->execute(@fieldvals) or die "Cannot execute: " . $sth->errstr();
Thanks for your wisdom,
digger

Replies are listed 'Best First'.
Re: DBD:CSV and creating a database
by Cabrion (Friar) on Jan 31, 2003 at 02:34 UTC
    I agree it should have bitched to high heaven when the SQL didn't parse correctly. In the past I have found that SQL::Statment is not very mature and you need to keep it up to date. Old versions are VERY buggy.

    For what it's worth: I always build my SQL in a scalar before sending it to prepare(). Then I can print it out while debugging to make sure what you built is what you expected. Typically I code against Postgres and will simply copy and paste the "debug" statement into the DB's shell to make sure it does what I think, a luxury you don't have with DBD::CSV.

    My code often looks like this when I am debugging it:

    my $sql = "create table x as (y text)"; #DEBUG (it's this output that I copy/paste) print "$sql\n"; #or perhaps die "$sql\n"; $dbh->do($sql) or die $dbh->errstr() ...
    If your only inserting data to a CSV file (as opposed to doing selects, etc) you might consider just using Text::CSV_XS which eliminates a lot of complexity/overhead.
Re: DBD:CSV and creating a database
by crouchingpenguin (Priest) on Jan 31, 2003 at 01:39 UTC
    if you add: mkdir($dir,0755) unless( -d $dir ); before your $dbh definition, and a comma after: phone    CHAR(14) it should create $dir . $table for you.

    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
      The directory already exists, so I was just checking for the file before trying to add data. It appears I simply missed the comma.

      Is it wrong of me to expect that my create table statment would die and report an error that would be passed back thru Carp? Am I missing something about the way Carp works?

      Besides opening my sleep deprived eyes and catching the missing comma, what else could I have done to trap this error? I would prefer to catch these errors myself than bug other people when I overlook the obvious.

      Gratefully,
      digger

Re: DBD:CSV and creating a database
by Anonymous Monk on Jan 31, 2003 at 04:51 UTC
    after phone CHAR(14) you forgot a comma....also "use DBI;" at start of prog if you haven't already. chris_piechowicz@hotmail.com