Compressed SQL Statements with PurePerl MySQL API
Using the PurePerl MySQL API– Part 12
Foreword: In this part of the series I explain how to use PurePerl MySQL API to compress a SQL statement and send to the MySQL server.
By: Date Published: 28 Aug 2017
Introduction
Compressing an INSERT Statement
The following code compresses an INSERT statement and send to MySQL server:
#!C:/Perl/bin/perl5.18.2.exe
use Mysql;
use strict;
if (!Mysql::connect("root", "secret", "localhost", 3306, 'compress'))
{
print "$Mysql::Error_msg";
}
else
{
if (Mysql::query("USE PetStore") != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
my $ins = "INSERT INTO pet (name, owner, species, sex, birth, death) VALUES ('Pin','Peop','brancher','m','2017-04-21',NULL)";
if (Mysql::query($ins) != 1)
{
print $Mysql::Error_msg, "\n";
}
}
}
Mysql::close();
This code is similar to other code samples, except for the presence of the argument, 'compress' in the connection expression (above).
The following code compresses a SELECT statement and send:
#!C:/Perl/bin/perl5.18.2.exe
use Mysql;
use strict;
if (!Mysql::connect("root", "secret", "localhost", 3306, 'compress'))
{
print "$Mysql::Error_msg";
}
else
{
if (Mysql::query("USE PetStore") != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
my $sel = "select * from pet where name='Pin'";
if (Mysql::query($sel) != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
for (my $i=0; $i<$Mysql::No_of_Rows; ++$i)
{
print $Mysql::Result[$i]{'name'}, ', ';
print $Mysql::Result[$i]{'owner'}, ', ';
print $Mysql::Result[$i]{'species'}, ', ';
print $Mysql::Result[$i]{'sex'}, ', ';
print $Mysql::Result[$i]{'birth'}, ', ';
print $Mysql::Result[$i]{'death'}, ', ';
print "\n";
}
}
}
}
Mysql::close();
Note the use of the 'compress' argument in the connection expression, which enabes the SELECT statement to be compressed (before being sent over the network).
The following code compresses a DELETE statement and send:
#!C:/Perl/bin/perl5.18.2.exe
use Mysql;
use strict;
if (!Mysql::connect("root", "secret", "localhost", 3306, 'compress'))
{
print "$Mysql::Error_msg";
}
else
{
if (Mysql::query("USE PetStore") != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
my $sel = "delete from pet where name='Pin'";
if (Mysql::query($sel) != 1)
{
print $Mysql::Error_msg, "\n";
}
}
}
Mysql::close();
This code is similar to other code samples, except for the presence of the argument, 'compress' in the connection expression. Any compression of a SQL statement, needs this argument.
Note: The API does not yet support a combination of INSERT, SELECT and DELETE.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT