Database Reference
In-Depth Information
my
$eol
=
"\n"
;
GetOptions
(
# =i means an integer value is required after the option
# =s means a string value is required after the option
"help"
=>
\
$help
,
# print help message
"host|h=s"
=>
\
$host_name
,
# server host
"password|p=s"
=>
\
$password
,
# password
"port|P=i"
=>
\
$port_num
,
# port number
"socket|S=s"
=>
\
$socket_name
,
# socket name
"user|u=s"
=>
\
$user_name
,
# username
"execute|e=s"
=>
\
$stmt
,
# statement to execute
"table|t=s"
=>
\
$tbl_name
,
# table to export
"labels|l"
=>
\
$labels
,
# generate row of column labels
"delim=s"
=>
\
$delim
,
# column delimiter
"quote=s"
=>
\
$quote
,
# column quoting character
"eol=s"
=>
\
$eol
# end-of-line (record) delimiter
)
or
die
"$usage\n"
;
die
"$usage\n"
if
defined
(
$help
);
$db_name
=
shift
(
@ARGV
)
if
@ARGV
;
# One of --execute or --table must be specified, but not both
die
"You must specify a query or a table name\n\n$usage\n"
unless
defined
(
$stmt
)
||
defined
(
$tbl_name
);
die
"You cannot specify both a query and a table name\n\n$usage\n"
if
defined
(
$stmt
)
&&
defined
(
$tbl_name
);
# interpret special chars in the file structure options
$quote
=
interpret_option
(
$quote
);
$delim
=
interpret_option
(
$delim
);
$eol
=
interpret_option
(
$eol
);
The
interpret_option()
function (not shown) processes escape and hex sequences
for the
--delim
,
--quote
, and
--eol
options. It interprets
\n
,
\r
,
\t
, and
\0
as linefeed,
carriage return, tab, and the ASCII NUL character. It also interprets hex values, which
can be given in
0x
nn
form (for example,
0x0d
indicates a carriage return).
After processing the command-line options,
mysql_to_text.pl
constructs the data source
name (DSN) and connects to the MySQL server:
my
$dsn
=
"DBI:mysql:"
;
$dsn
.=
";database=$db_name"
if
$db_name
;
$dsn
.=
";host=$host_name"
if
$host_name
;
$dsn
.=
";port=$port_num"
if
$port_num
;
$dsn
.=
";mysql_socket=$socket_name"
if
$socket_name
;
# read [client] group parameters from standard option files
$dsn
.=
";mysql_read_default_group=client"
;
my
$conn_attrs
=
{
PrintError
=>
0
,
RaiseError
=>
1
,
AutoCommit
=>
1
};
my
$dbh
=
DBI
->
connect
(
$dsn
,
$user_name
,
$password
,
$conn_attrs
);