Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Survey script to Access Database


Jun 16, 2000, 8:27 AM

Views: 2302
Survey script to Access Database

I have a survey script that post to a txt file. What I really want though is a survey script that posts directly to an Access database. If there is a way to get automatically from a txt file to an Access database, that would be a good intermediate solution.

Many thanks


User / Moderator

Jun 15, 2000, 10:25 PM

Views: 2302
Re: Survey script to Access Database

Use the DBI suite of modules.

DBD::ODBC can handle the Access portion, and you can use DBD::CSV or DBD::RAM for the text file.

Depending on your text, the latter's probably overkill as you could get away with something like ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use DBI;
my $dbh = DBI->connect("dbi:ODBC:MyAccessDB") &#0124; &#0124; die $DBI::errstr;
my $sth = $dbh->prepare("INSERT INTO tables VALUES (?,?,?,?)") &#0124; &#0124; die $dbh->errstr;

open TEXT, "MyTextDB.txt" &#0124; &#0124; die "Can't open MyTextDB.txt: $!";
while(<TEXT> ) {
my(@values) = split /,/, 4;
$sth->execute(@values) &#0124; &#0124; die $sth->errstr;

... which would populate your Access db assuming it's four fields corresponded to four comma seperated fields in your text file.

You can download DBI, DBD::ODBC, and DBD::CSV via ppm (if you use ActiveState's perl) or CPAN (if you use one of the other varieties).