2

I was trying to understand why it is happening but I could not find it. SQL Queries executes twice.

piece of my script:

my $driver = "Pg"; # Driver Name
my $database = $ARGV[0]; # First argument will be database name
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = $ARGV[1]; # second argument will be username
my $password = $ARGV[2]; # third argument will be passwd
my $dbh = DBI->connect($dsn,$userid,$password,  {RaiseError => 1 }) or die $DBI::errstr;

sub f_createUser {
    if (scalar @_ != 3 ) {
        return;
    }
    my $query = $dbh->prepare(qq{ SELECT createUserWithPassword(?,?) });
    #my $result = $dbh->do($query) < 0 ? say "$DBI::errstr" : "# >>> Query worked successfully";
    $query->execute($_[1],$_[2]);
}

&f_createUser(@revelantArray);

When I execute script, I got:

DBD::Pg::st execute failed: ERROR: role "tesstt" already exists CONTEXT: SQL statement "CREATE USER tesstt WITH PASSWORD '123'" PL/pgSQL function createuserwithpassword(character varying,character varying) line 3 at EXECUTE statement at homework2.pl line 39, line 1. DBD::Pg::st execute failed: ERROR: role "tesstt" already exists CONTEXT: SQL statement "CREATE USER tesstt WITH PASSWORD '123'" PL/pgSQL function createuserwithpassword(character varying,character varying) line 3 at EXECUTE statement at homework2.pl line 39, line 1.

Why is this happening ? What am I missing ?

Thanks in advance...

1 Answer 1

6

You have PrintError => 1, which prints an error message on error.

You have RaiseError => 1, which throws an exception consisting of an error message. Uncaught exceptions are printed as they end the program.

Add PrintError => 0 to the options passed to DBI->connect.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your attention, do I have PrintError => 1 ? where do I have it ? Where am I supposed to add PrintError => 0, could you specify bit more ?
It's one of the connect's options. That's the default.
allrigth but how should I add to PrintError => 0 ? where am I supposed to that ?
Add it to connect's options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.