Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

(dkubb) Re: (2) Using Files w/DBD::CSV

by dkubb (Deacon)
on Mar 05, 2001 at 01:26 UTC ( [id://62131]=note: print w/replies, xml ) Need Help??


in reply to Using Files

Here's an example showing what you want to do with DBD::CSV:

#!/usr/bin/perl -w #Define the behaviour of DBI->connect for the entire script $ENV{DBI_DSN} ||= 'DBI:CSV(RaiseError=>1,AutoCommit=>1):'. join ';', "csv_eol=\n", 'csv_sep_char=|', 'csv_quote_char=', 'csv_escape_char='; use strict; use DBI; use CGI; my $statement = q{ UPDATE list SET title = ? , description = ? WHERE id = ? }; my $cgi = CGI->new; #Connect to the data source my $dbh = DBI->connect; $dbh->do( $statement, {}, $cgi->param('title'), $cgi->param('description'), $cgi->param('id'), ); $dbh->disconnect; print $cgi->header, 'Updated List'; __END__

Note: The above code assumes that your file name is "list". DBD::CSV can only process files without any extension. Also, if this is going into production you might want turn on Taint checking by appending a T to the first line, as well as validating each of the CGI paramters, possibly using HTML::FormValidator, which is my personal favorite for this.

The great thing about using DBD::CSV is that if you ever switch to using a relational database all the code won't need to change, assuming your schema stays the same. This is why I explicitly define the environment variable $ENV{DBI_DSN} at the top of the script, so that you can easily change it later. Keep in mind this varible globally defines the data source that all the DBI->connect method calls will use, and can be globally set for all your CGI's in Apache's httpd.conf.

As well, I do suggest you look into relational databases, like MySQL or PostgreSQL. They might be better suited for storing this type of information than a flat file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://62131]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-04-19 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found