how do you replace fields on a table using perl?

martinthurn

New member
Your question is nonsequitur given this code. This program dies if it can not create the table; in other words, if the table already exists, the program will just die. Therefore it is impossible for an entry to already be in the table, as you ask.

But if you intend to massively overhaul this program... Just take a step back from the Perl code and figure out how you would do it in SQL -- after all, the Perl program is just sending SQL to the database! So, first you query for the data in question. If you get an answer, you know the entry is already there so you do UPDATE. If you get no answer, the entry is not there so you do an INSERT.
 
Hi, I got the code below, but I would like to check if an entry is already in the table and if not insert and if it is update it.

--CODE--


use strict;
use warnings;
use DBI;
use DBD::AnyData;

my $table = "data";

my @fields = qw/ id name /;
my %field_def = (
id => 'char(20)',
name => 'char(20)',
);

my $dbh = DBI->connect('dbi:AnyData(RaiseError=>1):')
or die "Can not create database connection";

# build the create table SQL on the fly
$dbh->do (
"CREATE TABLE $table (" .
join(',', map { $_ . ' ' . $field_def{$_} } @fields) .
")" )
or die "Can not create table";

$dbh->do("INSERT INTO $table VALUES ('01', 'FOO' )");


unlink './data.csv'; # delete existing csv file if any
$dbh->func( 'data', 'CSV', './data.csv', 'ad_export');

$dbh->disconnect();

--END CODE--

I've tried loads of different ways I've seen on websites, but none seen to work. I think its something to do with the "INSERT INTO" but, but I can't work it out.

I would also like to know if it is passable to make it so I can have unlimited letters it the fields?

Thanks for your help.
 
Back
Top