in reply to Re^4: inserting a column into mySQL DB with perl
in thread inserting a column into mySQL DB with perl
I'm not sure what your Cars table represents but this code updates the Cars table with idSize from a text file by matching idCars.
poj#!perl use strict; use DBI; my $dbh = dbh(); # connect my $sql = 'UPDATE cars SET Size = ? WHERE idCars = ?'; my $sth = $dbh->prepare($sql); # update while (<DATA>){ # or filehandle chomp; my ($idCars,$idSize) = split /\s+/,$_; $sth->execute($idSize,$idCars); } # show result my $ar = $dbh->selectall_arrayref('SELECT idCars,Car,Size FROM Cars'); printf "%5s %-15s %5s\n",@$_ for @$ar; # connect sub dbh{ my $database = "database"; my $user = "user"; my $pw = "password"; my $dsn = "dbi:mysql:$database:localhost:3306"; my $dbh = DBI->connect($dsn, $user, $pw, { RaiseError=>1, AutoCommit=>1 } ); return $dbh; } __DATA__ 1 1 2 2 3 3 4 3
|
|---|