Prepared statements use several data structures:
To prepare a statement, pass the MYSQL
connection handler along with the statement string to
mysql_stmt_init()
, which
returns a pointer to a MYSQL_STMT
data
structure.
To provide input parameters for a prepared statement, set up
MYSQL_BIND
structures and pass them to
mysql_stmt_bind_param()
. To
receive output column values, set up
MYSQL_BIND
structures and pass them to
mysql_stmt_bind_result()
.
The MYSQL_TIME
structure is used to
transfer temporal data in both directions.
The following discussion describes the prepared statement data types in detail.
This structure represents a prepared statement. A statement is
created by calling
mysql_stmt_init()
, which
returns a statement handle (that is, a pointer to a
MYSQL_STMT
). The handle is used for all
subsequent operations with the statement until you close it
with mysql_stmt_close()
, at
which point the handle becomes invalid.
The MYSQL_STMT
structure has no members
that are intended for application use. Also, you should not
try to make a copy of a MYSQL_STMT
structure. There is no guarantee that such a copy will be
usable.
Multiple statement handles can be associated with a single connection. The limit on the number of handles depends on the available system resources.
This structure is used both for statement input (data values sent to the server) and output (result values returned from the server):
For input, MYSQL_BIND
is used with
mysql_stmt_bind_param()
to
bind parameter data values to buffers for use by
mysql_stmt_execute()
.
For output, MYSQL_BIND
is used with
mysql_stmt_bind_result()
to bind result set buffers for use in fetching rows with
mysql_stmt_fetch()
.
To use a MYSQL_BIND
structure, you should
zero its contents to initialize it, and then set its members
appropriately. For example, to declare and initialize an array
of three MYSQL_BIND
structures, use this
code:
MYSQL_BIND bind[3]; memset(bind, 0, sizeof(bind));
The MYSQL_BIND
structure contains the
following members for use by application programs. For several
of the members, the manner of use depends on whether the
structure is used for input or output.
enum enum_field_types buffer_type
The type of the buffer. This member indicates the data
type of the C language variable that you are binding to
the statement parameter. The allowable
buffer_type
values are listed later in
this section. For input, buffer_type
indicates the type of the variable containing the value
that you will send to the server. For output, it indicates
the type of the variable into which you want a value
received from the server to be stored.
void *buffer
A pointer to the buffer to be used for data transfer. This is the address of a variable.
For input, buffer
is a pointer to the
variable in which a statement parameter's data value is
stored. When you call
mysql_stmt_execute()
,
MySQL takes the value that you have stored in the variable
and uses it in place of the corresponding parameter marker
in the statement.
For output, buffer
is a pointer to the
variable in which to return a result set column value.
When you call
mysql_stmt_fetch()
, MySQL
returns a column value and stores it in this variable. You
can access the value when the call returns.
To minimize the need for MySQL to perform type conversions
between C language values on the client side and SQL
values on the server side, use variables that have types
similar to those of the corresponding SQL values. For
numeric data types, buffer
should point
to a variable of the proper numeric C type. (For
char
or integer variables, you should
also indicate whether the variable has the
unsigned
attribute by setting the
is_unsigned
member, described later in
this list.) For character (nonbinary) and binary string
data types, buffer
should point to a
character buffer. For date and time data types,
buffer
should point to a
MYSQL_TIME
structure.
See the notes about type conversions later in the section.
unsigned long buffer_length
The actual size of *buffer
in bytes.
This indicates the maximum amount of data that can be
stored in the buffer. For character and binary C data, the
buffer_length
value specifies the
length of *buffer
when used with
mysql_stmt_bind_param()
to
specify input values, or the maximum number of output data
bytes that can be fetched into the buffer when used with
mysql_stmt_bind_result()
.
unsigned long *length
A pointer to an unsigned long
variable
that indicates the actual number of bytes of data stored
in *buffer
. length
is used for character or binary C data.
For input parameter data binding,
length
points to an unsigned
long
variable that indicates the actual length
of the parameter value stored in
*buffer
; this is used by
mysql_stmt_execute()
.
For output value binding, the return value of
mysql_stmt_fetch()
determines the interpretation of the length:
If mysql_stmt_fetch()
returns 0, *length
indicates the
actual length of the parameter value.
If mysql_stmt_fetch()
returns MYSQL_DATA_TRUNCATED
,
*length
indicates the nontruncated
length of the parameter value. In this case, the
minimum of *length
and
buffer_length
indicates the actual
length of the value.
length
is ignored for numeric and
temporal data types because the length of the data value
is determined by the buffer_type
value.
If you need to be able to determine the length of a
returned value before fetching it with
mysql_stmt_fetch()
, see
Section 20.8.7.11, “mysql_stmt_fetch()
”, for some strategies.
my_bool *is_null
This member points to a my_bool
variable that is true if a value is
NULL
, false if it is not
NULL
. For input, set
*is_null
to true to indicate that you
are passing a NULL
value as a statement
parameter.
The reason that is_null
is not a
boolean scalar but is instead a
pointer to a boolean scalar is to
provide flexibility in how you specify
NULL
values:
If your data values are always
NULL
, use
MYSQL_TYPE_NULL
as the
buffer_type
value when you bind the
column. The other members do not matter.
If your data values are always NOT
NULL
, set the other members appropriately
for the variable you are binding, and set
is_null = (my_bool*) 0
.
In all other cases, set the other members
appriopriately, and set is_null
to
the address of a my_bool
variable.
Set that variable's value to true or false
appropriately between executions to indicate whether
data values are NULL
or
NOT NULL
, respectively.
For output, the value pointed to by
is_null
is set to true after you fetch
a row if the result set column value returned from the
statement is NULL
.
my_bool is_unsigned
This member is used for C variables with data types that
can be unsigned
(char
, short int
,
int
, long long int
).
Set is_unsigned
to true if the variable
pointed to by buffer
is
unsigned
and false otherwise. For
example, if you bind a signed char
variable to buffer
, specify a type code
of MYSQL_TYPE_TINY
and set
is_unsigned
to false. If you bind an
unsigned char
instead, the type code is
the same but is_unsigned
should be
true. (For char
, it is not defined
whether it is signed or unsigned, so it is best to be
explicit about signedness by using signed
char
or unsigned char
.)
is_unsigned
applies only to the C
language variable on the client side. It indicates nothing
about the signedness of the corresponding SQL value on the
server side. For example, if you use an
int
variable to supply a value for a
BIGINT UNSIGNED
column,
is_unsigned
should be false because
int
is a signed type. If you use an
unsigned int
variable to supply a value
for a BIGINT
column,
is_unsigned
should be true because
unsigned int
is an unsigned type. MySQL
performs the proper conversion between signed and unsigned
values in both directions, although a warning occurs if
truncation results.
my_bool *error
For output, set this member to point to a
my_bool
variable to have truncation
information for the parameter stored there after a row
fetching operation. (Truncation reporting is enabled by
default, but can be controlled by calling
mysql_options()
with the
MYSQL_REPORT_DATA_TRUNCATION
option.)
When truncation reporting is enabled,
mysql_stmt_fetch()
returns
MYSQL_DATA_TRUNCATED
and
*error
is true in the
MYSQL_BIND
structures for parameters in
which truncation occurred. Truncation indicates loss of
sign or significant digits, or that a string was too long
to fit in a column. The error
member
was added in MySQL 5.0.3.
This structure is used to send and receive
DATE
,
TIME
,
DATETIME
, and
TIMESTAMP
data directly to and
from the server. Set the buffer_type
member
of a MYSQL_BIND
structure to one of the
temporal types (MYSQL_TYPE_TIME
,
MYSQL_TYPE_DATE
,
MYSQL_TYPE_DATETIME
,
MYSQL_TYPE_TIMESTAMP
), and set the
buffer
member to point to a
MYSQL_TIME
structure.
The MYSQL_TIME
structure contains the
members listed in the following table.
Member | Description |
unsigned int year |
The year |
unsigned int month |
The month of the year |
unsigned int day |
The day of the month |
unsigned int hour |
The hour of the day |
unsigned int minute |
The minute of the hour |
unsigned int second |
The second of the minute |
my_bool neg |
A boolean flag to indicate whether the time is negative |
unsigned long second_part |
The fractional part of the second in microseconds; currently unused |
Only those parts of a MYSQL_TIME
structure
that apply to a given type of temporal value are used. The
year
, month
, and
day
elements are used for
DATE
,
DATETIME
, and
TIMESTAMP
values. The
hour
, minute
, and
second
elements are used for
TIME
,
DATETIME
, and
TIMESTAMP
values. See
Section 20.8.14, “C API Prepared Statement Handling of Date and Time Values”.
The following table shows the allowable values that may be
specified in the buffer_type
member of
MYSQL_BIND
structures for input values. The
value should be chosen according to the data type of the C
language variable that you are binding. If the variable is
unsigned
, you should also set the
is_unsigned
member to true. The table shows the
C variable types that you can use, the corresponding type codes,
and the SQL data types for which the supplied value can be used
without conversion.
Input Variable C Type |
buffer_type Value
|
SQL Type of Destination Value |
signed char |
MYSQL_TYPE_TINY |
TINYINT |
short int |
MYSQL_TYPE_SHORT |
SMALLINT |
int |
MYSQL_TYPE_LONG |
INT |
long long int |
MYSQL_TYPE_LONGLONG |
BIGINT |
float |
MYSQL_TYPE_FLOAT |
FLOAT |
double |
MYSQL_TYPE_DOUBLE |
DOUBLE |
MYSQL_TIME |
MYSQL_TYPE_TIME |
TIME |
MYSQL_TIME |
MYSQL_TYPE_DATE |
DATE |
MYSQL_TIME |
MYSQL_TYPE_DATETIME |
DATETIME |
MYSQL_TIME |
MYSQL_TYPE_TIMESTAMP |
TIMESTAMP |
char[] |
MYSQL_TYPE_STRING (for nonbinary data) |
TEXT, CHAR, VARCHAR |
char[] |
MYSQL_TYPE_BLOB (for binary data) |
BLOB, BINARY, VARBINARY |
MYSQL_TYPE_NULL |
NULL |
The use of MYSQL_TYPE_NULL
is described earlier
in connection with the is_null
member.
The following table shows the allowable values that may be
specified in the buffer_type
member of
MYSQL_BIND
structures for output values. The
value should be chosen according to the data type of the C
language variable that you are binding. If the variable is
unsigned
, you should also set the
is_unsigned
member to true. The table shows the
SQL types of received values, the corresponding type code that
such values have in result set metadata, and the recommended C
language data types to bind to the MYSQL_BIND
structure to receive the SQL values without conversion.
If there is a mismatch between the C variable type on the client side and the corresponding SQL value on the server side, MySQL performs implicit type conversions in both directions.
SQL Type of Received Value |
buffer_type Value
|
Output Variable C Type |
TINYINT |
MYSQL_TYPE_TINY |
signed char |
SMALLINT |
MYSQL_TYPE_SHORT |
short int |
MEDIUMINT |
MYSQL_TYPE_INT24 |
int |
INT |
MYSQL_TYPE_LONG |
int |
BIGINT |
MYSQL_TYPE_LONGLONG |
long long int |
FLOAT |
MYSQL_TYPE_FLOAT |
float |
DOUBLE |
MYSQL_TYPE_DOUBLE |
double |
DECIMAL |
MYSQL_TYPE_NEWDECIMAL |
char[] |
YEAR |
MYSQL_TYPE_SHORT |
short int |
TIME |
MYSQL_TYPE_TIME |
MYSQL_TIME |
DATE |
MYSQL_TYPE_DATE |
MYSQL_TIME |
DATETIME |
MYSQL_TYPE_DATETIME |
MYSQL_TIME |
TIMESTAMP |
MYSQL_TYPE_TIMESTAMP |
MYSQL_TIME |
CHAR, BINARY |
MYSQL_TYPE_STRING |
char[] |
VARCHAR, VARBINARY |
MYSQL_TYPE_VAR_STRING |
char[] |
TINYBLOB, TINYTEXT |
MYSQL_TYPE_TINY_BLOB |
char[] |
BLOB, TEXT |
MYSQL_TYPE_BLOB |
char[] |
MEDIUMBLOB, MEDIUMTEXT |
MYSQL_TYPE_MEDIUM_BLOB |
char[] |
LONGBLOB, LONGTEXT |
MYSQL_TYPE_LONG_BLOB |
char[] |
BIT |
MYSQL_TYPE_BIT |
char[] |
MySQL knows the type code for the SQL value on the server side.
The buffer_type
value indicates the MySQL the
type code of the C variable that holds the value on the client
side. The two codes together tell MySQL what conversion must be
performed, if any. Here are some examples:
If you use MYSQL_TYPE_LONG
with an
int
variable to pass an integer value to
the server that is to be stored into a
FLOAT
column, MySQL converts
the value to floating-point format before storing it.
If you fetch an SQL MEDIUMINT
column value, but specify a buffer_type
value of MYSQL_TYPE_LONGLONG
and use a C
variable of type long long int
as the
destination buffer, MySQL will convert the
MEDIUMINT
value (which requires
less than 8 bytes) for storage into the long long
int
(an 8-byte variable).
If you fetch a numeric column with a value of 255 into a
char[4]
character array and specify a
buffer_type
value of
MYSQL_TYPE_STRING
, the resulting value in
the array will be a 4-byte string containing
'255\0'
.
DECIMAL
values are returned as
strings, which is why the corresponding C type is
char[]
.
DECIMAL
values returned by the
server correspond to the string representation of the original
server-side value. For example, 12.345
is
returned to the client as '12.345'
. If you
specify MYSQL_TYPE_NEWDECIMAL
and bind a
string buffer to the MYSQL_BIND
structure,
mysql_stmt_fetch()
stores the
value in the buffer without conversion. If instead you specify
a numeric variable and type code,
mysql_stmt_fetch()
converts
the string-format DECIMAL
value
to numeric form.
For the MYSQL_TYPE_BIT
type code,
BIT
values are returned into a
string buffer (thus, the corresponding C type is
char[]
here, too). The value represents a
bit string that requires interpretation on the client side. To
return the value as a type that is easier to deal with, you
can cause the value to be cast to integer using either of the
following types of expressions:
SELECT bit_col + 0 FROM t SELECT CAST(bit_col AS UNSIGNED) FROM t
To retrieve the value, bind an integer variable large enough to hold the value and specify the appropriate corresponding integer type code.
Before binding variables to the MYSQL_BIND
structures that are to be used for fetching column values, you can
check the type codes for each column of the result set. This might
be desirable if you want to determine which variable types would
be best to use to avoid type conversions. To get the type codes,
call mysql_stmt_result_metadata()
after executing the prepared statement with
mysql_stmt_execute()
. The metadata
provides access to the type codes for the result set as described
in Section 20.8.7.22, “mysql_stmt_result_metadata()
”, and
Section 20.8.1, “C API Data Types”.
If you cause the max_length
member of the
MYSQL_FIELD
column metadata structures to be
set (by calling
mysql_stmt_attr_set()
), be aware
that the max_length
values for the result set
indicate the lengths of the longest string representation of the
result values, not the lengths of the binary representation. That
is, max_length
does not necessarily correspond
to the size of the buffers needed to fetch the values with the
binary protocol used for prepared statements. The size of the
buffers should be chosen according to the types of the variables
into which you fetch the values.
For input character (nonbinary) string data (indicated by
MYSQL_TYPE_STRING
), the value is assumed to be
in the character set indicated by the
character_set_client
system
variable. If the value is stored into a column with a different
character set, the appropriate conversion to that character set
occurs. For input binary string data (indicated by
MYSQL_TYPE_BLOB
), the value is treated as
having the binary
character set; that is, it is
treated as a byte string and no conversion occurs.
To determine whether output string values in a result set returned
from the server contain binary or nonbinary data, check whether
the charsetnr
value of the result set metadata
is 63 (see Section 20.8.1, “C API Data Types”). If so, the
character set is binary
, which indicates binary
rather than nonbinary data. This enables you to distinguish
BINARY
from
CHAR
,
VARBINARY
from
VARCHAR
, and the
BLOB
types from the
TEXT
types.
User Comments
It seems the MYSQL_BIND structure is vastly different between 5.0 and 5.1. This means that apps compiled using 5.0 headers must be recompiled for 5.1.
Add your own comment.