I'm working with a MySQL database and need开发者_如何学Python to check if a row is already there before deciding which queries to run.
The table I'm working on is something like this:
player(playerid, nickname, data1, data2, data3)
Where "playerid" is an auto-incremented number and "nickname" is unique.
I tried some queries with COUNT, COALESCE, fetch->rows, etc.. and got nowhere. I already saw this question, but couldn't solve anything.
Could you please post the specific query with SELECT COUNT(*)
that didn't work and what the problem was?
The query should be modeled upon this answer: How do I know how many rows a Perl DBI query returns?
Assuming your "row is already there" definition is "the player with the given nickname is there", the query would be:
my $th = $dbh->prepare(qq{SELECT COUNT(1) FROM player WHERE nickname='$nickname'});
$th->execute();
if ($th->fetch()->[0]) {
....
} # Code stolen shamelessly from the link above
You can use some dbh/sth method to check whether the result set is empty:
my $stmt = 'SELECT playerid FROM player WHERE playerid = ?';
if ($dbh->selectrow_array($stmt, undef, $id)) {
print "The row exists";
}
精彩评论