I need my Perl script to do this:
- read a file (1 word per line)
- if that word is in the database it would count +1
- if not it must add it to the database
I came up with this Perl script, but it doesn't work (it doesn't really matter that this is a Perl script, I guess there a re some problems with my SQL statements...):
$dbh = DBI->connect( "DBI:mysql:$database_host", "$username", "$pass", {RaiseError=>1,AutoCommit=>1});
open(F, "file.txt"
while(<F>){
chomp;
$sth = $dbh->do(qq{UPDATE `words` SET count=count+1 WHERE `text` LIKE '$_';}) or
$sth = $dbh->do(qq{INSERT INTO `words` (`text`, `count`) VALUES ('$_', '1')});
}
close(F);
If I delete the "UPDATE" statement it works ( = adds the new values to the table), but I don't want the same words repeated in the table, I want the count to increment. Maybe have some other SQL to suggest?
Thanks for your help!
- read a file (1 word per line)
- if that word is in the database it would count +1
- if not it must add it to the database
I came up with this Perl script, but it doesn't work (it doesn't really matter that this is a Perl script, I guess there a re some problems with my SQL statements...):
$dbh = DBI->connect( "DBI:mysql:$database_host", "$username", "$pass", {RaiseError=>1,AutoCommit=>1});
open(F, "file.txt"
while(<F>){
chomp;
$sth = $dbh->do(qq{UPDATE `words` SET count=count+1 WHERE `text` LIKE '$_';}) or
$sth = $dbh->do(qq{INSERT INTO `words` (`text`, `count`) VALUES ('$_', '1')});
}
close(F);
If I delete the "UPDATE" statement it works ( = adds the new values to the table), but I don't want the same words repeated in the table, I want the count to increment. Maybe have some other SQL to suggest?
Thanks for your help!