in reply to Re^2: Updating or Inserting a database from a txt file
in thread Updating or Inserting a database from a txt file
Then, create a unique index on the table - the 200 is a length that you can modify if you want.$dbh->do("CREATE TABLE `games` (`gamename` TEXT, `gamedesc` TEXT, `gam +ecount` TEXT );");
$dbh->do("ALTER TABLE `games` ADD UNIQUE `UNQINDX` ( `gamename` ( 200 +) )");
I'm a bit tired, but I hope this helps! Note my comments within the code.my (@row, $gamename, $gamedesc, $gamecount); # selecting count(*) will run faster my $select = $dbh->prepare( "SELECT count(*) FROM `games` WHERE `gamen +ame`=?" ); # column `gamecount` was labeled as `gamecounter` my $insert = $dbh->prepare( "INSERT INTO `games` (`gamename`, `gamedes +c`, `gamecount`) VALUES (?, ?, ?)" ); my $update = $dbh->prepare( "UPDATE games SET gamedesc=?, gamecount=? +where gamename=?" ); open (FILE, "<../data/games/descriptions.txt") or die $!; while (<FILE>) { chomp; ($gamename, $gamedesc) = split /\t/; $select->execute( $gamename ); if ($select->fetchrow_array) { # WARNING: $gamecount isn't defined! $update->execute( $gamedesc, $gamecount, $gamename ); } else { $insert->execute( $gamename, $gamedesc, 0 ); } } close (FILE);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Updating or Inserting a database from a txt file
by Nik (Initiate) on May 12, 2005 at 10:22 UTC |