in reply to Data insert into SQL Server?
poj#$dbh->do('DROP TABLE test1'); # uncomment after first run my $sql = 'CREATE TABLE test1 ( type char(5), students int, class int, teachers int, hostel char(10) )'; $dbh->do($sql); my $sqli = 'INSERT INTO test1 VALUES (?,?,?,?,?)'; my $sth = $dbh->prepare($sqli); # 1 records my @fld=(); # read data foreach (<DATA>) { chomp $_; my ($event,$data)=split(/:/,$_); if ($event =~ /\(B\)/){ insert_record() if ($fld[0]); $fld[0] = "BOYS"; } if( $event =~ /\(G\)/){ insert_record() if ($fld[0]); $fld[0] = "GIRLS"; } if ($event =~ /^No.of stud/i){ $fld[1] = $data; } if($event =~ /^No.of class/i){ $fld[2] = $data; } if($event =~ /^No.of teach/i){ $fld[3] = $data; } if($event =~ /^Hostel facility/i){ $fld[4] = $data; } } # don't forget last record insert_record() if ($fld[0]); # insert one records sub insert_record { print "Inserting @fld\n"; $sth->execute(@fld[0..4]); @fld=(); } __DATA__ +++++++++++School(B):Students++++++++++ No.of students:120 No.of classes:2 No.of teachers:5 Hostel facility:available ++++++++++School(G):Students+++++++++++ No.of students:300 No.of classes:3 No.of teachers:10
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Data insert into SQL Server?
by gmoque (Acolyte) on Aug 08, 2011 at 02:17 UTC | |
by poj (Abbot) on Aug 08, 2011 at 18:21 UTC |