Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4534810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:19:37+00:00 2026-05-21T14:19:37+00:00

I have this following Perl script, i would like it to check if the

  • 0

I have this following Perl script, i would like it to check if the name, level, and area fields are, combined, a unique reference. If there is already an entry fitting those fields, ignore it and do nothing. If there isn’t an entry with these specifications, add it.

This one works, but doesn’t check if there is an unique entry already and adds without consideration

#! /usr/bin/perl -w
use DBI;
use DBD::mysql;
use Data::Dumper;
# MySQL Variables

my ($username, $password, $db, $login_info);
$username=""; #omitted
$password=""; #omitted
$db=''; #omitted
$table=''; #omitted
$dbh=DBI->connect("dbi:mysql:$db", $username, $password) or die "Connection Error: $DBI::errstr\n";

    my $insert=$dbh->do("INSERT INTO $table (area, name, level, align, hp, maxhp, bash, pierce, slash, acid, air, cold, disease, earth, fire, holy, light, electric, magic, mental, negative, poison, shadow, sonic, water) VALUES ('$ARGV[0]', '$ARGV[1]', '$ARGV[2]', '$ARGV[3]', '$ARGV[4]', '$ARGV[5]', '$ARGV[6]', '$ARGV[7]', '$ARGV[8]', '$ARGV[9]', '$ARGV[10]', '$ARGV[11]', '$ARGV[12]', '$ARGV[13]', '$ARGV[14]', '$ARGV[15]', '$ARGV[16]', '$ARGV[17]', '$ARGV[18]', '$ARGV[19]', '$ARGV[20]', '$ARGV[21]', '$ARGV[22]', '$ARGV[23]', '$ARGV[24]');");

The following was my attempt to make it check, which failed miserably. Any and all help would be appreciated.

#! /usr/bin/perl -w

use DBI;
#! /usr/bin/perl -w

use DBI;
use DBD::mysql;
use Data::Dumper;
# MySQL Variables

my ($username, $password, $db, $login_info);
$username=""; #omitted
$password=""; #omitted
$db=''; #omitted
$table=''; #omitted
$dbh=DBI->connect("dbi:mysql:$db", $username, $password) or die "Connection Error: $DBI::errstr\n";
my ($area, $name, $level) = ($ARGV[1], $ARGV[1], $ARGV[2]);

my $query=$dbh->prepare("SELECT name, area, level from Interrogate WHERE name=$name, area=$area, level=$level;");
$do=$query->execute(  );
if ($do) { 
    my $insert=$dbh->do("INSERT INTO $table (area, name, level, align, hp, maxhp, bash, pierce, slash, acid, air, cold, disease, earth, fire, holy, light, electric, magic, mental, negative, poison, shadow, sonic, water) VALUES ('$ARGV[0]', '$ARGV[1]', '$ARGV[2]', '$ARGV[3]', '$ARGV[4]', '$ARGV[5]', '$ARGV[6]', '$ARGV[7]', '$ARGV[8]', '$ARGV[9]', '$ARGV[10]', '$ARGV[11]', '$ARGV[12]', '$ARGV[13]', '$ARGV[14]', '$ARGV[15]', '$ARGV[16]', '$ARGV[17]', '$ARGV[18]', '$ARGV[19]', '$ARGV[20]', '$ARGV[21]', '$ARGV[22]', '$ARGV[23]', '$ARGV[24]');");
}

EDIT Removed extraneous curly brace.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:19:38+00:00Added an answer on May 21, 2026 at 2:19 pm

    You seem to be confused about what execute returns, from the fine manual:

    An undef is returned if an error occurs. A successful execute always returns true regardless of the number of rows affected, even if it’s zero (see below).
    […]
    The execute method does not return the number of rows that will be returned by the query (because most databases can’t tell in advance), it simply returns a true value.

    So you want to do something like this instead:

    my $query = $dbh->prepare("SELECT COUNT(*) FROM Interrogate WHERE name = ? AND area = ? AND level = ?");
    $query->execute($name, $area, $level);
    my $count = $query->fetchrow_arrayref();
    if(!$count->[0]) {
       # do the insert
    }
    

    I threw in the placeholders (to protect against SQL injection attacks) and corrected the syntax of your WHERE clause for free.

    Even better would be to put a unique index on (name,area,level) inside the database. Then you could just do the INSERT and trap and ignore the “unique constraint violated” exception. This approach protects against a race condition in your approach: you check for an existing row but don’t find one, another process adds the row, you add the row but end up with a duplicate because someone else inserted it between your check and your insert. Pushing data integrity issues down into the database is always your best bet, databases are good at things like that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following snippet calling a perl script which writes to STDERR and
I have a Perl script that needs to run following an annual schedule with
I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,
I have this following setup, a textarea named with some data in it that
I have this following code, here CssClass is of the String type. <asp:Panel ID=Panel1
I have this following numpy matrix that I want to sort in ascending order
I have this following code which will return all the current semesters. How do
I have following this blog in setting Xvfb in my ubuntu environment: http://corpocrat.com/2008/08/19/how-to-install-xvfb-x11-server-in-linux-server/ So
Never thought I'd have this problem :) The following snippet of code works in
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.