Subject: | Update statements does not work properly |
I've experienced some problems with 'UPDATE' statements:
sample one:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $st_create = qq~CREATE TABLE classes (class_id INTEGER NOT NULL,
class_name VARCHAR(255) NOT NULL, subs INTEGER(1), vars INTEGER(1) )~;
my $st_insert = qq~INSERT INTO classes(class_id,class_name,subs,vars)
VALUES(?,?,?,?)~;
my $st_update = qq~UPDATE classes SET subs = ? WHERE class_id = ?~;
my @classes = (
[1,'Class::One',1,1],
[2,'Class::Two',1,1],
);
my $database = DBI->connect('DBI:CSV:f_dir=../Test') or die
$DBI::errstr;
$database->do($st_create) or die $database->errstr();
for my $class(@classes){
my $sth = $database->prepare($st_insert) or die $database->errstr();
$sth->execute(@$class) or die $database->errstr();
$sth->finish();
}
my @updates = ([0,1],[0,2]);
for my $update(@updates){
my $sth = $database->prepare($st_update) or die $database->errstr();
$sth->execute(@$update) or die $database->errstr();
$sth->finish();
}
makes the following 'classes'-file:
class_id,class_name,subs,vars
1,Class::One,1,1
2,Class::Two,2,1
sample 2:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $st_create = qq~CREATE TABLE classes (class_id INTEGER NOT NULL,
class_name VARCHAR(255) NOT NULL, subs INTEGER(1), vars INTEGER(1) )~;
my $st_insert = qq~INSERT INTO classes(class_id,class_name,subs,vars)
VALUES(?,?,?,?)~;
my $st_update = qq~UPDATE classes SET subs = ? WHERE class_id = ?~;
my @classes = (
[1,'Class::One',1,1],
[2,'Class::Two',1,1],
);
my $database = DBI->connect('DBI:CSV:f_dir=../Test') or die
$DBI::errstr;
$database->do($st_create) or die $database->errstr();
for my $class(@classes){
my $sth = $database->prepare($st_insert) or die $database->errstr();
$sth->execute(@$class) or die $database->errstr();
$sth->finish();
}
my @updates = ([0,1],[5,2]);
for my $update(@updates){
$database->do($st_update,undef,@$update) or die $database->errstr();
}
==> classes:
class_id,class_name,subs,vars
1,Class::One,0,1
2,Class::Two,2,1