# “Update the Prices in a Database!”

## use DBI;

##

## $db = DBI->connect('dbi:ODBC:PRICE', 'user', 'password',

## { RaiseError => 1 });

## $upd = $db->prepare('UPDATE prices SET price=? WHERE prod=?');

## $ins = $db->prepare('INSERT INTO prices(prod,price) VALUES (?,?)');

##

## while ($line = <COST>) {

## . . .

## $rows = $upd->execute($price, $prod);

## $ins->execute($prod,$price) if $rows == 0;

## }

## $db->commit;

Previous slide | Next slide | Back to first slide | View graphic version

Notes:

Here we add just 8 lines to store the prices in the database.

The code not only updates prices for products already in the database but also inserts new records for any products not in the database.