Reputation: 1943
In my perl program I have a function which takes a query as input and executes it and prints any rows fetched if the query is a select statement.
But the problem is with update or insert statements where fetchrow_array gives an error so is there any way i can know what type of query was executed so that i can skip the fetch part?
Upvotes: 0
Views: 118
Reputation: 1246
Create a little helper function using a regular expressions to check the SQL string for specific words in the query...
my $sql = "UPDATE some_table SET some_value = 'abc' WHERE some_value = 'xyz'";
sub get_query_type
{
my $sql_string = shift;
my $query_type;
if( $sql_string =~ m/^UPDATE/i )
{
$query_type = "An UPDATE query";
# do UPDATE stuff
}
elsif( $sql_string =~ m/^INSERT/i )
{
$query_type = "An INSERT query";
# do INSERT stuff
}
# more blocks here as needed.
return $query_type;
}
my $type = get_query_type( $sql );
print $type;
Upvotes: 0
Reputation: 22972
Not knowing what sort of SQL you are executing sounds like a bad idea to me, but that's not what you asked, so...
Check the docs and you will find:
You can tell if the statement was a SELECT statement by checking if $sth->{NUM_OF_FIELDS} is greater than zero after calling execute.
Upvotes: 5