Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
MariaDB
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
MariaDB
Commits
a7a3a286
Commit
a7a3a286
authored
Apr 02, 2003
by
arjen@george.bitbike.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
prepare.texi merged into manual (mysqldoc tree), file removed.
parent
8cbad532
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
0 additions
and
1736 deletions
+0
-1736
Docs/prepare.texi
Docs/prepare.texi
+0
-1736
No files found.
Docs/prepare.texi
deleted
100755 → 0
View file @
8cbad532
\input
texinfo @c -*-texinfo-*-
@c Copyright 1997-2002 TcX AB, Detron HB and MySQL Finland AB
@c
@c This manual is NOT distributed under a GPL style license.
@c Use of the manual is subject to the following terms:
@c - Conversion to other formats is allowed, but the actual
@c content may not be altered or edited in any way.
@c - You may create a printed copy for your own personal use.
@c - For all other uses, such as selling printed copies or
@c using (parts of) the manual in another publication,
@c prior written agreement from MySQL AB is required.
@c
@c Please e-mail docs@mysql.com for more information or if
@c you are interested in doing a translation.
@c
@c *********************************************************
@c Note that @node names are used on our web site.
@c So do not change node names without checking
@c Makefile.am and SitePages first.
@c *********************************************************
@c
@c
%**start of header
@c there's a better way to do this... i just don't know it yet
@c sed will remove the "@c ifnusphere " to make this valid
@c ifnusphere @set nusphere 1
@setfilename prepare.info
@c We want the types in the same index
@c @syncodeindex tp fn
@c Get version information. This file is generated by the Makefile!!
@include include.texi
@ifclear tex-debug
@c This removes the black squares in the right margin
@finalout
@end ifclear
@c Set background for HTML
@set
_
body
_
tags BGCOLOR=#FFFFFF TEXT=#000000 LINK=#101090 VLINK=#7030B0
@c Set some style elements for the manual in HTML form. 'suggested'
@c natural language colors: aqua, black, blue, fuchsia, gray, green,
@c lime, maroon, navy, olive, purple, red, silver, teal, white, and
@c yellow. From Steeve Buehler <ahr@YogElements.com>
@set
_
extra
_
head <style> code
{
color:purple
}
tt
{
color:green
}
samp
{
color:navy
}
pre
{
color:maroon
}
</style>
@settitle MySQL Prepared Statements
@c We want single-sided heading format, with chapters on new pages. To
@c get double-sided format change 'on' below to 'odd'
@ifclear nusphere
@setchapternewpage on
@end ifclear
@ifset nusphere
@setchapternewpage odd
@end ifset
@c @paragraphindent 0
@ifset nusphere
@smallbook
@end ifset
@c @titlepage
@c @sp 10
@c @center @titlefont
{
MySQL Prepared Statements
}
@c @sp 10
@c @right Copyright @copyright
{}
1995-2003 MySQL AB
@c blank page after title page makes page 1 be a page front.
@c also makes the back of the title page blank.
@c @page
@c @end titlepage
@c Short contents, blank page, long contents.
@c until i can figure out the blank page, no short contents.
@c @shortcontents
@c @page
@c @page
@contents
@c This should be added. The HTML conversion also needs a MySQL version
@c number somewhere.
@iftex
@c change this to double if you want formatting for double-sided
@c printing
@headings single
@end iftex
@c @node Top, MySQL C API, (dir), (dir)
@c @menu
@c * MySQL C API::
@c @end menu
@c @node MySQL C API, , Top, Top
@c @chapter MySQL C API
@c @menu
@c * Prepared statements::
@c @end menu
@node Top, MySQL prepared statements, (dir), (dir)
@menu
* MySQL prepared statements::
@end menu
@node MySQL prepared statements, , Top, Top
@chapter MySQL Prepared Statements
@menu
* C Prepared statements::
* C Prepared statement datatypes::
* C Prepared statements function overview::
* C Prepared statement functions::
* multiple queries::
* date handling::
@end menu
@node C Prepared statements, C Prepared statement datatypes, MySQL prepared statements, MySQL prepared statements
@subsection C Prepared Statements
@sp 1
From MySQL 4.1 and above, you can also make use of the prepared
statements using the statement handler 'MYSQL
_
STMT', which supports
simultanious query executions along with input and output binding.
@sp 1
Prepared execution is an efficient way to execute a statement more than
once. The statement is first parsed, or prepared. This is executed one
or more times at a later time using the statement handle that is
returned during the prepare.
@sp 1
Another advantage of prepared statements is that, it uses a binary protocol
which makes the data tranfer between client and server in a more efficient
way than the old MySQL protocol.
@sp 1
Prepared execution is faster than direct execution for statements
executed more than once, primarly becuase the query is parsed only
once; In the case of direct execution, the query is parsed every
time. Prepared execution also can provide a reduction in the network
traffic becuase during the execute call, it only sends the data for the
parameters.
@node C Prepared statement datatypes, C Prepared statements function overview, C Prepared statements, MySQL prepared statements
@subsection C Prepared Statements DataTypes
Prepared statements mainly uses the following two @code
{
MYSQL
_
STMT
}
and
@code
{
MYSQL
_
BIND
}
structures:
@sp 1
@table @code
@tindex MYSQL
_
STMT C type
@item MYSQL
_
STMT
This structure represents a statement handle to prepared statements.It
is used for all statement related functions.
@sp 1
The statement is initialized when the query is prepared using
@code
{
mysql
_
prepare()
}
.
@sp 1
One connection can have 'n' statement handles, and the limit depends up on
the system resources.
@sp 1
@tindex MYSQL
_
BIND C type
@item MYSQL
_
BIND
This structure is used in order to bind parameter
buffers(@code
{
mysql
_
bind
_
param()
}
) inorder to the parameters data to
@code
{
mysql
_
execute()
}
call; as well as to bind row
buffers(@code
{
mysql
_
bind
_
result()
}
) to fetch the result set data using
@code
{
mysql
_
fetch()
}
.
@sp 1
@noindent
The @code
{
MYSQL
_
BIND
}
structure contains the members listed here:
@table @code
@item enum enum
_
field
_
types buffer
_
type [input]
The type of the buffer. The @code
{
type
}
value must be one of the following:
@itemize @bullet
@item @code
{
MYSQL
_
TYPE
_
TINY
}
@item @code
{
MYSQL
_
TYPE
_
SHORT
}
@item @code
{
MYSQL
_
TYPE
_
LONG
}
@item @code
{
MYSQL
_
TYPE
_
LONGLONG
}
@item @code
{
MYSQL
_
TYPE
_
FLOAT
}
@item @code
{
MYSQL
_
TYPE
_
DOUBLE
}
@item @code
{
MYSQL
_
TYPE
_
TIME
}
@item @code
{
MYSQL
_
TYPE
_
DATE
}
@item @code
{
MYSQL
_
TYPE
_
DATETIME
}
@item @code
{
MYSQL
_
TYPE
_
TIMESTAMP
}
@item @code
{
MYSQL
_
TYPE
_
STRING
}
@item @code
{
MYSQL
_
TYPE
_
VAR
_
STRING
}
@item @code
{
MYSQL
_
TYPE
_
TINY
_
BLOB
}
@item @code
{
MYSQL
_
TYPE
_
MEDIUM
_
BLOB
}
@item @code
{
MYSQL
_
TYPE
_
LONG
_
BLOB
}
@item @code
{
MYSQL
_
TYPE
_
BLOB
}
@end itemize
@sp 1
@item void *buffer [input/output]
A pointer to a buffer for the parameters data in case if it is used to
supply parameters data or pointer to a buffer in which to return the
data when the structure is used for result set bind.
@sp 1
@item unsigned long buffer
_
length [input]
Length of the @code
{
*buffer
}
in bytes. For character and binary C data,
the buffer
_
length specifies the length of the @code
{
*buffer
}
to be used
as a parameter data in case if it is used with @code
{
mysql
_
bind
_
param()
}
or to return that many bytes when fetching results when this is used
with @code
{
mysql
_
bind
_
result()
}
.
@item long *length [input/output]
Pointer to the buffer for the parameter's length. When the structure is
used as a input parameter data binding, then this argument points to a
buffer that, when @code
{
mysql
_
execute()
}
is called, contains the length
of the parameter value stored in *buffer. This is ignored except for
character or binary C data.
If the length is a null pointer, then the protocol assumes that all
character and binary data are null terminated.
When this structure is used in output binding, then @code
{
mysql
_
fetch()
}
return the the length of the data that is returned.
@sp 1
@item bool *is
_
null [input/output]
Indicates if the parameter data is NULL or fetched data is NULL.
@end table
@sp 1
@tindex MySQL C type
@item MYSQL
_
TIME
This structure is used to send and receive DATE, TIME and
TIMESTAMP data directly to/from server.
@sp 1
@noindent
The @code
{
MYSQL
_
TIME
}
structure contains the members listed here:
@multitable @columnfractions .20 .20 .68
@item @strong
{
Member
}
@tab @strong
{
Type
}
@tab @strong
{
Description
}
@item @code
{
year
}
@tab unsigned int @tab Year.
@item @code
{
month
}
@tab unsigned int @tab Month of the year.
@item @code
{
day
}
@tab unsigned int @tab Day of the month.
@item @code
{
hour
}
@tab unsigned int @tab Hour of the day(TIME).
@item @code
{
minute
}
@tab unsigned int @tab Minute of the hour.
@item @code
{
second
}
@tab unsigned int @tab Second of the minute.
@item @code
{
neg
}
@tab my
_
bool @tab A boolean flag to
indicate if the time is negative.
@item @code
{
second
_
part
}
@tab unsigned long @tab Fraction part of the
second(not yet used)
@end multitable
@end table
@node C Prepared statements function overview, C Prepared statement functions, C Prepared statement datatypes, MySQL prepared statements
@subsection C Prepared Statements Function Overview
@cindex C Prepared statements API, functions
@cindex functions, C Prepared statements API
The functions available in the prepared statements are listed here and
are described in greater detail in the later section.
@xref
{
C Prepared statement functions
}
.
@multitable @columnfractions .32 .68
@item @strong
{
Function
}
@tab @strong
{
Description
}
@item @strong
{
mysql
_
prepare()
}
@tab Prepares an SQL string for execution.
@item @strong
{
mysql
_
param
_
count()
}
@tab Returns the number of parameters in a prepared SQL statement.
@item @strong
{
mysql
_
prepare
_
result()
}
@tab Returns prepared statement meta information in the form of resultset.
@item @strong
{
mysql
_
bind
_
param()
}
@tab Binds a buffer to parameter markers in a prepared SQL statement.
@item @strong
{
mysql
_
execute()
}
@tab Executes the prepared statement.
@item @strong
{
mysql
_
stmt
_
affected
_
rows()
}
@tab Returns the number of rows changes/deleted/inserted by the last UPDATE,DELETE,or INSERT query
@item @strong
{
mysql
_
bind
_
result()
}
@tab Binds application data buffers
to columns in the resultset.
@item @strong
{
mysql
_
stmt
_
store
_
result()
}
@tab Retrieves the complete result set to the client
@item @strong
{
mysql
_
fetch()
}
@tab Fetches the next rowset of data from the result set and returns data for all bound columns.
@item @strong
{
mysql
_
stmt
_
close()
}
@tab Frees memory used by prepared statement.
@item @strong
{
mysql
_
stmt
_
errno()
}
@tab Returns the error number for the last statement execution.
@item @strong
{
mysql
_
stmt
_
error()
}
@tab Returns the error message for the last statement execution.
@item @strong
{
mysql
_
send
_
long
_
data()
}
@tab Sends long data in chunks to server.
@c TO BE MOVED TO MAIN C API FUCTIONS
@item @strong
{
mysql
_
commit()
}
@tab Commits the transaction.
@item @strong
{
mysql
_
rollback()
}
@tab Rollbacks the transaction.
@item @strong
{
mysql
_
autocommit()
}
@tab Toggles the autocommit mode to on/off.
@item @strong
{
mysql
_
more
_
results()
}
@tab Returns if there are any more results exists
@item @strong
{
mysql
_
next
_
result()
}
@tab Returns/Initiates the next result in the multi-query executions
@end multitable
@sp 1
Call @code
{
mysql
_
prepare()
}
to prepare and initialize the statement
handle, then call @code
{
mysql
_
bind
_
param()
}
to supply the parameters
data, and then call @code
{
mysql
_
execute()
}
to execute the query. You can
repeat the @code
{
mysql
_
execute()
}
by changing parameter values from the
respective buffer supplied through @code
{
mysql
_
bind
_
param()
}
.
@sp 1
In case if the query is a SELECT statement or any other query which
results in a resultset, then mysql
_
prepare() will also return the result
set meta data information in the form of @code
{
MYSQL
_
RES
}
result set
through @code
{
mysql
_
prepare
_
result()
}
.
@sp 1
You can supply the result buffers using @code
{
mysql
_
bind
_
result()
}
, so
that the @code
{
mysql
_
fetch()
}
will automatically returns data to this
buffers. This is row by row fetching.
@sp 1
You can also send the text or binary data in chunks to server using
@code
{
mysql
_
send
_
long
_
data()
}
, by specifying the option is
_
long
_
data=1
or length=MYSQL
_
LONG
_
DATA or -2 in the MYSQL
_
BIND structure supplied
with @code
{
mysql
_
bind
_
param()
}
.
@sp 1
Once the statement execution is over, it must be freed using
@code
{
mysql
_
stmt
_
close
}
so that it frees all the alloced resources for
the statement handle.
@subsubheading Execution Steps:
To prepare and execute a statement, the application:
@itemize @bullet
@item
Calls @strong
{
mysql
_
prepare()
}
and passes it a string containing the SQL
statement. On a successful prepare, mysql
_
prepare returns the valid statement
handle back to the application
@item
If the query results in a resultset, then @strong
{
mysql
_
prepare
_
result
}
returns the result set meta info..
@item
Sets the values of any parameters using @strong
{
mysql
_
bind
_
param
}
. All
parameters must be set; else it will return an error or produce
un-expected results
@item
Calls @strong
{
mysql
_
execute
}
to execute the statement.
@item
Repeat steps 2 and 3 as necessary, by changing the parameter values and
re-executing the statement.
@item
Bind the data buffers to return the row values, if it is a result set
query; using @strong
{
mysql
_
bind
_
result()
}
.
@item
Fetch the data to buffers row by row by calling @strong
{
mysql
_
fetch()
}
repetedely until no more rows found.
@item
When @strong
{
mysql
_
prepare()
}
is called, in the MySQL client/server protocol:
@itemize @minus
@item
Server parses the query and sends the ok status back to client by
assinging a statement id. It also sends total number of parameters,
columns count and its meta information if it is a result set oriented
query. All syntax and symantecs of the query is checked during this call
by the server.
@item
Client uses this statement id for the further executions, so that server
identifies the statement back from the pool of statements. Now, client
allocates a statement handle with this id and returns back to
application.
@end itemize
@item
When @strong
{
mysql
_
execute()
}
is called, in the MySQL client/server protocol:
@itemize @minus
@item
Client uses the statement handle and sends the parameters data to
server.
@item
Server identifies the statement using the id provided by the client, and
replaces the parameter markers with the newly supplied data and executes
the query. If it results in a result set, then sends the data back to
client, else sends an OK status with total number of rows
changes/deleted/inserted.
@end itemize
@item
When @strong
{
mysql
_
fetch()
}
is called, in the MySQL client/server protocol:
@itemize @minus
@item
Client reads the data from the packet row by row and places it to
application data buffers by doing the necessary conversions. If the
application buffer type is same as that of field type, then the
conversions are stright forward.
@end itemize
@end itemize
You can get the statement error code and message using
@code
{
mysql
_
stmt
_
errno()
}
and @code
{
mysql
_
stmt
_
error()
}
respectively.
@node C Prepared statement functions, multiple queries, C Prepared statements function overview, MySQL prepared statements
@subsection C Prepared Statement Function Descriptions
You need to use the following functions when you want to prepare and
execute the queries.
@menu
* mysql
_
prepare:: @code
{
mysql
_
prepare()
}
* mysql
_
param
_
count:: @code
{
mysql
_
param
_
count()
}
* mysql
_
prepare
_
result:: @code
{
mysql
_
prepare
_
result()
}
* mysql
_
bind
_
param:: @code
{
mysql
_
bind
_
param()
}
* mysql
_
execute:: @code
{
mysql
_
execute()
}
* mysql
_
stmt
_
affected
_
rows:: @code
{
mysql
_
stmt
_
affected
_
rows()
}
* mysql
_
bind
_
result:: @code
{
mysql
_
bind
_
result()
}
* mysql
_
stmt
_
store
_
result:: @code
{
mysql
_
stmt
_
store
_
result()
}
* mysql
_
fetch:: @code
{
mysql
_
fetch()
}
* mysql
_
send
_
long
_
data:: @code
{
mysql
_
send
_
long
_
data()
}
* mysql
_
stmt
_
close:: @code
{
mysql
_
stmt
_
close()
}
* mysql
_
stmt
_
errno:: @code
{
mysql
_
stmt
_
errno()
}
* mysql
_
stmt
_
error:: @code
{
mysql
_
stmt
_
error()
}
* mysql
_
commit:: @code
{
mysql
_
commit()
}
* mysql
_
rollback:: @code
{
mysql
_
rollback()
}
* mysql
_
autocommit:: @code
{
mysql
_
autocommit()
}
* mysql
_
more
_
results:: @code
{
mysql
_
more
_
results()
}
* mysql
_
next
_
result:: @code
{
mysql
_
next
_
result()
}
@end menu
@node mysql
_
prepare, mysql
_
param
_
count, C Prepared statement functions, C Prepared statement functions
@subsubsection @code
{
mysql
_
prepare()
}
@findex @code
{
mysql
_
prepare()
}
@code
{
MYSQL
_
STMT * mysql
_
prepare(MYSQL *mysql, const char *query, unsigned
long length)
}
@subsubheading Description
Prepares the SQL query pointed to by the null-terminated string
'query'. The query must consist of a single SQL statement. You should
not add a terminating semicolon (`;`) or
\g
to the statement.
@sp 1
The application can include one or more parameter markers in the SQL
statement. To include a parameter marker, the appication embeds a
question mark (@code
{
?
}
) into the SQL string at the appropriate
position.
@sp 1
The markers are legal only in certain places in SQL statements. For
example, they are not allowed in the select list(the list of columns to
be returned by a SELECT statement), nor are they allowed as both
operands of a binary operator such as the equal sign (=), becuase it
would be impossible to determine the parameter type. In general,
parameters are legal only in Data Manipulation Languange(DML)
statements, and not in Data Defination Language(DDL) statements.
@sp 1
The parameter markers are then bound to application variables using
@code
{
mysql
_
bind
_
param()
}
.
@subsubheading Return Values
@code
{
MYSQL
_
STMT
}
if the prepare was successful. NULL if an error
occured.
@subsubheading Errors
@item CR
_
COMMANDS
_
OUT
_
OF
_
SYNC
Commands were executed in an improper order
@item CR
_
OUT
_
OF
_
MEMORY
Out of memory
@item CR
_
SERVER
_
GONE
_
ERROR
The MySQL server has gone away
@item CR
_
SERVER
_
LOST
The connection to the server was lost during the query
@item CR
_
UNKNOWN
_
ERROR
An unkown error occured
@end table
If the prepare is not successful, i.e. when @code
{
mysql
_
prepare()
}
returned a
NULL statement, errors can be obtained by calling @code
{
mysql
_
error()
}
.
@subsubheading Example
For the usage of @code
{
mysql
_
prepare()
}
refer to the Example from
@ref
{
mysql
_
execute,mysql
_
execute()
}
.
@node mysql
_
param
_
count, mysql
_
prepare
_
result, mysql
_
prepare, C Prepared statement functions
@subsubsection @code
{
mysql
_
param
_
count()
}
@findex @code
{
mysql
_
param
_
count()
}
@code
{
unsigned int mysql
_
param
_
count(MYSQL
_
STMT *stmt)
}
@subsubheading Description
Returns the number of parameter markers present from the prepared query.
@subsubheading Return Values
An unsigned integer representing the number of parameters in a
statement.
@subsubheading Errors
None
@subsubheading Example
For the usage of @code
{
mysql
_
param
_
count()
}
refer to the Example from
@ref
{
mysql
_
execute,mysql
_
execute()
}
.
@node mysql
_
prepare
_
result, mysql
_
bind
_
param, mysql
_
param
_
count, C Prepared statement functions
@subsubsection @code
{
mysql
_
prepare
_
result()
}
@findex @code
{
mysql
_
prepare
_
result
}
.
@code
{
MYSQL
_
RES *mysql
_
prepare
_
result(MYSQL
_
STMT *stmt)
}
@subsubheading Description
If the @code
{
mysql
_
prepare()
}
resulted in a result set query, then
@code
{
mysql
_
prepare
_
result()
}
returns the result set meta data in the form of
@code
{
MYSQL
_
RES
}
structure; which can further be used to process the
meta information such as total number of fields and individual field
information. This resulted result set can be passed as an argument to
any of the field based APIs in order to process the result set meta data
information such as:
@itemize @minus
@item
mysql
_
num
_
fields()
@item
mysql
_
fetch
_
field()
@item
mysql
_
fetch
_
field
_
direct()
@item
mysql
_
fetch
_
fields()
@item
mysql
_
field
_
count()
@item
mysql
_
field
_
seek()
@item
mysql
_
field
_
tell() and
@item
mysql
_
free
_
result()
@end itemize
@subsubheading Return Values
A @code
{
MYSQL
_
RES
}
result structure. NULL if no meta information exists from
the prepared query.
@subsubheading Errors
@item CR
_
OUT
_
OF
_
MEMOR
Out of memory
@item CR
_
UNKNOWN
_
ERROR
An unknown error occured
None
@subsubheading Example
For the usage of @code
{
mysql
_
prepare
_
result()
}
refer to the Example from
@ref
{
mysql
_
fetch,mysql
_
fetch()
}
@node mysql
_
bind
_
param, mysql
_
execute, mysql
_
prepare
_
result, C Prepared statement functions
@subsubsection @code
{
mysql
_
bind
_
param()
}
@findex @code
{
mysql
_
bind
_
param()
}
@code
{
int mysql
_
bind
_
param(MYSQL
_
STMT *stmt, MYSQL
_
BIND *bind)
}
@subsubheading Description
@code
{
mysql
_
bind
_
param
}
is used to bind data for the parameter markers
in the SQL statement from @code
{
mysql
_
prepare
}
. It uses the structure
MYSQL
_
BIND to supply the data.
The supported buffer types are:
@itemize @bullet
@item
MYSQL
_
TYPE
_
TINY
@item
MYSQL
_
TYPE
_
SHORT
@item
MYSQL
_
TYPE
_
LONG
@item
MYSQL
_
TYPE
_
LONGLONG
@item
MYSQL
_
TYPE
_
FLOAT
@item
MYSQL
_
TYPE
_
DOUBLE
@item
MYSQL
_
TYPE
_
TIME
@item
MYSQL
_
TYPE
_
DATE
@item
MYSQL
_
TYPE
_
DATETIME
@item
MYSQL
_
TYPE
_
TIMESTAMP
@item
MYSQL
_
TYPE
_
STRING
@item
MYSQL
_
TYPE
_
VAR
_
STRING
@item
MYSQL
_
TYPE
_
TINY
_
BLOB
@item
MYSQL
_
TYPE
_
MEDIUM
_
BLOB
@item
MYSQL
_
TYPE
_
LONG
_
BLOB
@end itemize
@subsubheading Return Values
Zero if the bind was successful. Non-zero if an error occured.
@subsubheading Errors
@table @code
@item CR
_
NO
_
PREPARE
_
STMT
No prepared statement exists
@item CR
_
NO
_
PARAMETERS
_
EXISTS
No parameters exists to bind
@item CR
_
INVALID
_
BUFFER
_
USE
Indicates if the bind is to supply the long data in chunks and if the
buffer type is non string or binary
@item CR
_
UNSUPPORTED
_
PARAM
_
TYPE
The conversion is not supported, possibly the buffer
_
type is illegal or
its not from the above list of supported types.
@item CR
_
OUT
_
OF
_
MEMOR
Out of memory
@item CR
_
UNKNOWN
_
ERROR
An unknown error occured
@end table
@subsubheading Example
For the usage of @code
{
mysql
_
bind
_
param()
}
refer to the Example from
@ref
{
mysql
_
execute,mysql
_
execute()
}
.
@node mysql
_
execute, mysql
_
stmt
_
affected
_
rows, mysql
_
bind
_
param, C Prepared statement functions
@subsubsection @code
{
mysql
_
execute()
}
@findex @code
{
mysql
_
execute()
}
@code
{
int mysql
_
execute(MYSQL
_
STMT *stmt
}
.
@subsubheading Description
@code
{
mysql
_
execute()
}
executes the prepared query associated with the
statement handle. The parameter marker values will be sent to server
during this call, so that server replaces markers with this newly
supplied data.
@sp 1
If the statement is UPDATE,DELETE,or INSERT, the total number of
changed/deletd/inserted values can be found by calling
@code
{
mysql
_
stmt
_
affected
_
rows
}
. If this is a result set query, then one
must call @code
{
mysql
_
fetch()
}
to fetch the data prior to calling any
other calls which results in query processing. For more information on
how to fetch the statement binary data, refer to @ref
{
mysql
_
fetch
}
.
@subsubheading Return Values
@code
{
mysql
_
execute()
}
returns the following return values:
@multitable @columnfractions .30 .65
@item @strong
{
Return Value
}
@tab @strong
{
Description
}
@item 0 @tab Successful
@item 1 @tab Error occured. Error code and
message can be obtained by calling @code
{
mysql
_
stmt
_
errno()
}
and @code
{
mysql
_
stmt
_
error()
}
.
@end multitable
@subsubheading Errors
@table @code
@item CR
_
NO
_
PREPARE
_
QUERY
No query prepared prior to execution
@item CR
_
ALL
_
PARAMS
_
NOT
_
BOUND
Not all parameters data is supplied
@item CR
_
COMMANDS
_
OUT
_
OF
_
SYNC
Commands were executed in an improper order.
@item CR
_
OUT
_
OF
_
MEMORY
Out of memory.
@item CR
_
SERVER
_
GONE
_
ERROR
The MySQL server has gone away.
@item CR
_
SERVER
_
LOST
The connection to the server was lost during the query.
@item CR
_
UNKNOWN
_
ERROR
An unknown error occurred.
@end table
@subsubheading Example
The following example explains the uasage of @code
{
mysql
_
prepare
}
,
@code
{
mysql
_
param
_
count
}
, @code
{
mysql
_
bind
_
param
}
, @code
{
mysql
_
execute
}
and @code
{
mysql
_
stmt
_
affected
_
rows()
}
.
@example
MYSQL
_
BIND bind[3];
MYSQL
_
STMT *stmt;
ulonglong affected
_
rows;
long length;
unsigned int param
_
count;
int int
_
data;
short small
_
data;
char str
_
data[50], query[255];
my
_
bool is
_
null;
/* Set autocommit mode to true */
mysql
_
autocommit(mysql, 1);
if (mysql
_
query(mysql,"DROP TABLE IF EXISTS test
_
table"))
@
{
fprintf(stderr, "
\n
drop table failed");
fprintf(stderr, "
\n
%s", mysql_error(mysql));
exit(0);
@
}
if (mysql
_
query(mysql,"CREATE TABLE test
_
table(col1 int, col2 varchar(50),
\
col3 smallint,
\
col4 timestamp(14))"))
@
{
fprintf(stderr, "
\n
create table failed");
fprintf(stderr, "
\n
%s", mysql_error(mysql));
exit(0);
@
}
/* Prepare a insert query with 3 parameters */
strmov(query, "INSERT INTO test
_
table(col1,col2,col3) values(?,?,?)");
if(!(stmt = mysql
_
prepare(mysql, query, strlen(query))))
@
{
fprintf(stderr, "
\n
prepare, insert failed");
fprintf(stderr, "
\n
%s", mysql_error(mysql));
exit(0);
@
}
fprintf(stdout, "
\n
prepare, insert successful");
/* Get the parameter count from the statement */
param
_
count= mysql
_
param
_
count(stmt);
fprintf(stdout, "
\n
total parameters in insert:
%d", param_count);
if (param
_
count != 3) /* validate parameter count */
@
{
fprintf(stderr, "
\n
invalid parameter count returned by MySQL");
exit(0);
@
}
/* Bind the data for the parameters */
/* INTEGER PART */
bind[0].buffer
_
type= MYSQL
_
TYPE
_
LONG;
bind[0].buffer= (char *)
&
int
_
data;
bind[0].is
_
null= 0;
bind[0].length= 0;
/* STRING PART */
bind[1].buffer
_
type= MYSQL
_
TYPE
_
VAR
_
STRING;
bind[1].buffer= (char *)str
_
data;
bind[1].buffer
_
length= sizeof(str
_
data);
bind[1].is
_
null= 0;
bind[1].length= 0;
/* SMALLINT PART */
bind[2].buffer
_
type= MYSQL
_
TYPE
_
SHORT;
bind[2].buffer= (char *)
&
small
_
data;
bind[2].is
_
null=
&
is
_
null;
bind[2].length= 0;
is
_
null= 0;
/* Bind the buffers */
if (mysql
_
bind
_
param(stmt, bind))
@
{
fprintf(stderr, "
\n
param bind failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
/* Specify the data */
int
_
data= 10; /* integer */
strcpy(str
_
data,"MySQL"); /* string */
/* INSERT SMALLINT data as NULL */
is
_
null= 1;
/* Execute the insert statement - 1*/
if (mysql
_
execute(stmt))
@
{
fprintf(stderr, "
\n
execute 1 failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
fprintf(stderr, "
\n
send a bug report to bugs@@lists.mysql.com, by asking why this is not working ?");
exit(0);
@
}
/* Get the total rows affected */
affected
_
rows= mysql
_
stmt
_
affected
_
rows(stmt);
fprintf(stdout, "
\n
total affected rows:
%lld", affected_rows);
if (affected
_
rows != 1) /* validate affected rows */
@
{
fprintf(stderr, "
\n
invalid affected rows by MySQL");
exit(0);
@
}
/* Re-execute the insert, by changing the values */
int
_
data= 1000;
strcpy(str
_
data,"The most popular open source database");
small
_
data= 1000; /* smallint */
is
_
null= 0; /* reset NULL */
/* Execute the insert statement - 2*/
if (mysql
_
execute(stmt))
@
{
fprintf(stderr, "
\n
execute 2 failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
/* Get the total rows affected */
affected
_
rows= mysql
_
stmt
_
affected
_
rows(stmt);
fprintf(stdout, "
\n
total affected rows:
%lld", affected_rows);
if (affected
_
rows != 1) /* validate affected rows */
@
{
fprintf(stderr, "
\n
invalid affected rows by MySQL");
exit(0);
@
}
/* Close the statement */
if (mysql
_
stmt
_
close(stmt))
@
{
fprintf(stderr, "
\n
failed while closing the statement");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
/* DROP THE TABLE */
if (mysql
_
query(mysql,"DROP TABLE test
_
table"))
@
{
fprintf(stderr, "
\n
drop table failed");
fprintf(stderr, "
\n
%s", mysql_error(mysql));
exit(0);
@
}
fprintf(stdout, "Success, MySQL prepared statements are working!!!");
@end example
@node mysql
_
stmt
_
affected
_
rows, mysql
_
bind
_
result, mysql
_
execute, C Prepared statement functions
@subsubsection @code
{
mysql
_
stmt
_
affected
_
rows()
}
@findex @code
{
mysql
_
stmt
_
affected
_
rows()
}
@code
{
ulonglong mysql
_
stmt
_
affected
_
rows(MYSQL
_
STMT *stmt)
}
@subsubheading Description
Returns total number of rows changed by the last execute statement. May
be called immediatlely after mysql
_
execute() for UPDATE,DELETE,or INSERT
statements.For SELECT statements, mysql
_
stmt
_
affected rows works like
mysql
_
num
_
rows().
@subsubheading Return Values
An integer greater than zero indicates the number of rows affected or
retrieved. Zero indicates that no records where updated for an UPDATE
statement, no rows matched the WHERE clause in the query or that no
query has yet been executed. -1 indicates that the query returned an
error or that, for a SELECT query, mysql
_
stmt
_
affected
_
rows() was called
prior to calling mysql
_
fetch().
@subsubheading Errors
None.
@subsubheading Example
For the usage of @code
{
mysql
_
stmt
_
affected
_
rows()
}
refer to the Example
from @ref
{
mysql
_
execute,mysql
_
execute()
}
.
@node mysql
_
bind
_
result, mysql
_
stmt
_
store
_
result, mysql
_
stmt
_
affected
_
rows, C Prepared statement functions
@subsubsection @code
{
mysql
_
bind
_
result()
}
@findex @code
{
mysql
_
bind
_
result()
}
@code
{
my
_
bool mysql
_
bind
_
result(MYSQL
_
STMT *stmt, MYSQL
_
BIND *bind)
}
@subsubheading Description
@code
{
mysql
_
bind
_
result()
}
is used to associate, or bind, columns in the
resultset to data buffers and length buffers. When @code
{
mysql
_
fetch()
}
is
called to fetch data, the MySQL client protocol returns the data for the
bound columns in the specified buffers.
@sp 1
Note that all columns must be bound prior to calling @code
{
mysql
_
fetch()
}
in case of fetching the data to buffers; else @code
{
mysql
_
fetch()
}
simply ignores
the data fetch; also the buffers should be sufficient enough to hold the
data as the ptotocol doesn't return the data in chunks.
@sp 1
A column can be bound or rebound at any time, even after data has been
fetched from the result set. The new binding takes effect the next time
@code
{
mysql
_
fetch()
}
is called. For example, suppose an application binds
the columns in a result set and calls @code
{
mysql
_
fetch()
}
. The mysql
protocol returns data in the bound buffers. Now suppose the application
binds the columns to a different set of buffers, then the protocol does
not place the data for the just fetched row in the newly bound
buffers. Instead, it does when the next @code
{
mysql
_
fetch()
}
is called.
@sp 1
To bind a column, an application calls @code
{
mysql
_
bind
_
result()
}
and
passes the type, address, and the address of the length buffer.
The supported buffer types are:
@itemize @bullet
@item
MYSQL
_
TYPE
_
TINY
@item
MYSQL
_
TYPE
_
SHORT
@item
MYSQL
_
TYPE
_
LONG
@item
MYSQL
_
TYPE
_
LONGLONG
@item
MYSQL
_
TYPE
_
FLOAT
@item
MYSQL
_
TYPE
_
DOUBLE
@item
MYSQL
_
TYPE
_
TIME
@item
MYSQL
_
TYPE
_
DATE
@item
MYSQL
_
TYPE
_
DATETIME
@item
MYSQL
_
TYPE
_
TIMESTAMP
@item
MYSQL
_
TYPE
_
STRING
@item
MYSQL
_
TYPE
_
VAR
_
STRING
@item
MYSQL
_
TYPE
_
BLOB
@item
MYSQL
_
TYPE
_
TINY
_
BLOB
@item
MYSQL
_
TYPE
_
MEDIUM
_
BLOB
@item
MYSQL
_
TYPE
_
LONG
_
BLOB
@end itemize
@subsubheading Return Values
Zero if the bind was successful. Non-zero if an error occured.
@subsubheading Errors
@table @code
@item CR
_
NO
_
PREPARE
_
STMT
No prepared statement exists
@item CR
_
UNSUPPORTED
_
PARAM
_
TYPE
The conversion is not supported, possibly the buffer
_
type is illegal or
its not from the list of supported types.
@item CR
_
OUT
_
OF
_
MEMOR
Out of memory
@item CR
_
UNKNOWN
_
ERROR
An unknown error occured
@end table
@subsubheading Example
For the usage of @code
{
mysql
_
bind
_
result()
}
refer to the Example from
@ref
{
mysql
_
fetch,mysql
_
fetch()
}
@node mysql
_
stmt
_
store
_
result, mysql
_
fetch, mysql
_
bind
_
result, C Prepared statement functions
@subsubsection @code
{
mysql
_
stmt
_
store
_
result()
}
@findex code
{
mysql
_
stmt
_
store
_
result()
}
@code
{
int mysql
_
stmt
_
store
_
result(MYSQL
_
STMT *stmt)
}
@subsubheading Description
You must call @code
{
mysql
_
stmt
_
store
_
result()
}
for every query that
successfully retrieves
data(@code
{
SELECT
}
,@code
{
SHOW
}
,@code
{
DESCRIBE
}
,@code
{
EXPLAIN
}
), and only
if you want to buffer the complete result set by the client, so that the
subsequent @code
{
mysql
_
fetch()
}
call returns buffered data.
@sp 1
You don't have to call @code
{
mysql
_
stmt
_
store
_
result()
}
for other
queries, but it will not harm or cause any notable performance in all
cases.You can detect if the query didn't have a result set by checking
if @code
{
mysql
_
prepare
_
result()
}
returns 0. For more information refer
to @ref
{
mysql
_
prepare
_
result
}
.
@subsubheading Return Values
@code
{
Zero
}
if the results are buffered successfully or @code
{
Non Zero
}
in case of an error.
@subsubheading Errors
@table @code
@item CR
_
COMMANDS
_
OUT
_
OF
_
SYNC
Commands were executed in an improper order.
@item CR
_
OUT
_
OF
_
MEMORY
Out of memory.
@item CR
_
SERVER
_
GONE
_
ERROR
The MySQL server has gone away.
@item CR
_
SERVER
_
LOST
The connection to the server was lost during the query.
@item CR
_
UNKNOWN
_
ERROR
An unknown error occurred.
@end table
@node mysql
_
fetch, mysql
_
send
_
long
_
data, mysql
_
stmt
_
store
_
result, C Prepared statement functions
@subsubsection @code
{
mysql
_
fetch()
}
@findex code
{
mysql
_
fetch()
}
@code
{
int mysql
_
fetch(MYSQL
_
STMT *stmt)
}
@subsubheading Description
@code
{
mysql
_
fetch()
}
returns the next rowset in the result set. It can
be called only while the result set exists i.e. after a call to
@code
{
mysql
_
execute()
}
that creates a result set or after
@code
{
mysql
_
stmt
_
store
_
result()
}
, which is called after
@code
{
mysql
_
execute()
}
to buffer the entire resultset.
@sp 1
If row buffers are bound using @code
{
mysql
_
bind
_
result()
}
, it returns
the data in those buffers for all the columns in the current row
set and the lengths are returned to the length pointer.
@sp 1
Note that, all columns must be bound by the application.
@sp 1
If the data fetched is a NULL data, then the @code
{
is
_
null
}
value from
@code
{
MYSQL
_
BIND
}
contains TRUE, 1, else the data and its length is
returned to @code
{
*buffer
}
and @code
{
*length
}
variables based on the
buffer type specified by the application. All numeric, float and double
types have the fixed length(in bytes) as listed below:
@multitable @columnfractions .10 .30
@item @strong
{
Type
}
@tab @strong
{
Length
}
@item MYSQL
_
TYPE
_
TINY @tab 1
@item MYSQL
_
TYPE
_
SHORT @tab 2
@item MYSQL
_
TYPE
_
LONG @tab 4
@item MYSQL
_
TYPE
_
FLOAT @tab 4
@item MYSQL
_
TYPE
_
LONGLONG @tab 8
@item MYSQL
_
TYPE
_
DOUBLE @tab 8
@item MYSQL
_
TYPE
_
TIME @tab sizeof(MYSQL
_
TIME)
@item MYSQL
_
TYPE
_
DATE @tab sizeof(MYSQL
_
TIME)
@item MYSQL
_
TYPE
_
DATETIME @tab sizeof(MYSQL
_
TIME)
@item MYSQL
_
TYPE
_
TIMESTAMP @tab sizeof(MYSQL
_
TIME)
@item MYSQL
_
TYPE
_
STRING @tab data length
@item MYSQL
_
TYPE
_
VAR
_
STRING @tab data
_
length
@item MYSQL
_
TYPE
_
BLOB @tab data
_
length
@item MYSQL
_
TYPE
_
TINY
_
BLOB @tab data
_
length
@item MYSQL
_
TYPE
_
MEDIUM
_
BLOB @tab data
_
length
@item MYSQL
_
TYPE
_
LONG
_
BLOB @tab data
_
length
@end multitable
@*
where @code
{
*data
_
length
}
is nothing but the 'Actual length of the data'.
@subsubheading Return Values
@multitable @columnfractions .30 .65
@item @strong
{
Return Value
}
@tab @strong
{
Description
}
@item 0 @tab Successful, the data has been
fetched to application data buffers.
@item 1 @tab Error occured. Error code and
message can be obtained by calling @code
{
mysql
_
stmt
_
errno()
}
and @code
{
mysql
_
stmt
_
error()
}
.
@item 100, MYSQL
_
NO
_
DATA @tab No more rows/data exists
@end multitable
@subsubheading Errors
@table @code
@item CR
_
COMMANDS
_
OUT
_
OF
_
SYNC
Commands were executed in an improper order.
@item CR
_
OUT
_
OF
_
MEMORY
Out of memory.
@item CR
_
SERVER
_
GONE
_
ERROR
The MySQL server has gone away.
@item CR
_
SERVER
_
LOST
The connection to the server was lost during the query.
@item CR
_
UNKNOWN
_
ERROR
An unknown error occurred.
@item CR
_
UNSUPPORTED
_
PARAM
_
TYPE
If the buffer type is MYSQL
_
TYPE
_
DATE,DATETIME,TIME,or TIMESTAMP; and if
the field type is not DATE, TIME, DATETIME or TIMESTAMP.
@item
All other unsupported conversion errors are returned from
@code
{
mysql
_
bind
_
result()
}
.
@end table
@subsubheading Example
The following example explains the usage of @code
{
mysql
_
prepare
_
result
}
,
@code
{
mysql
_
bind
_
result()
}
, and @code
{
mysql
_
fetch()
}
@example
MYSQL
_
STMT *stmt;
MYSQL
_
BIND bind[2];
MYSQL
_
RES *result;
int int
_
data;
long int
_
length, str
_
length;
char str
_
data[50];
my
_
bool is
_
null[2];
query= "SELECT col1, col2 FROM test
_
table WHERE col1= 10)");
if (!(stmt= mysql
_
prepare(
&
mysql, query, strlen(query)))
@
{
fprintf(stderr, "
\n
prepare failed");
fprintf(stderr, "
\n
%s", mysql_error(&stmt));
exit(0);
@
}
/* Get the fields meta information */
if (!(result= mysql
_
prepare
_
result(stmt)))
@
{
fprintf(stderr, "
\n
prepare
_
result failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
fprintf(stdout, "Total fields:
%ld", mysql_num_fields(result));
if (mysql
_
num
_
fields(result) != 2)
@
{
fprintf(stderr, "
\n
prepare returned invalid field count");
exit(0);
@
}
/* Execute the SELECT query */
if (mysql
_
execute(stmt))
@
{
fprintf(stderr, "
\n
execute failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
/* Bind the result data buffers */
bind[0].buffer
_
type= MYSQL
_
TYPE
_
LONG;
bind[0].buffer= (char *)
&
int
_
data;
bind[0].is
_
null=
&
is
_
null[0];
bind[0].length=
&
int
_
length;
bind[1].buffer
_
type= MYSQL
_
TYPE
_
VAR
_
STRING;
bind[1].buffer= (void *)str
_
data;
bind[1].is
_
null=
&
is
_
null[1];
bind[1].length=
&
str
_
length;
if (mysql
_
bind
_
result(stmt, bind))
@
{
fprintf(stderr, "
\n
bind
_
result failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
/* Now fetch data to buffers */
if (mysql
_
fetch(stmt))
@
{
fprintf(stderr, "
\n
fetch failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
if (is
_
null[0])
fprintf(stdout, "
\n
Col1 data is NULL");
else
fprintf(stdout, "
\n
Col1:
%d, length: %ld", int_data, int_length);
if (is
_
null[1])
fprintf(stdout, "
\n
Col2 data is NULL");
else
fprintf(stdout, "
\n
Col2:
%s, length: %ld", str_data, str_length);
/* call mysql
_
fetch again */
if (mysql
_
fetch(stmt) |= MYSQL
_
NO
_
DATA)
@
{
fprintf(stderr, "
\n
fetch return more than one row);
exit(0);
@
}
/* Free the prepare result meta information */
mysql
_
free
_
result(result);
/* Free the statement handle */
if (mysql
_
stmt
_
free(stmt))
@
{
fprintf(stderr, "
\n
failed to free the statement handle);
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
@end example
@node mysql
_
send
_
long
_
data, mysql
_
stmt
_
close, mysql
_
fetch, C Prepared statement functions
@subsubsection @code
{
mysql
_
send
_
long
_
data()
}
@findex @code
{
mysql
_
send
_
long
_
data()
}
.
@code
{
int mysql
_
send
_
long
_
data(MYSQL
_
STMT *stmt, unsigned int
parameter
_
number, const char *data, ulong length)
}
@subsubheading Description
Allows an application to send the data in pieces or chunks to
server. This function can be used to send character or binary data
values in parts to a column(it must be a text or blob) with a character or
binary data type.
@sp 1
The @code
{
data
}
is a pointer to buffer containing the actual data for
the parameter represendted by @code
{
parameter
_
number
}
. The @code
{
length
}
indicates the amount of data to be sent in bytes.
@subsubheading Return Values
Zero if the data is sent successfully to server. Non-zero if an error
occured.
@subsubheading Errors
@table @code
@item CR
_
INVALID
_
PARAMETER
_
NO
Invalid parameter number
@item CR
_
COMMANDS
_
OUT
_
OF
_
SYNC
Commands were executed in an improper order.
@item CR
_
SERVER
_
GONE
_
ERROR
The MySQL server has gone away
@item CR
_
OUT
_
OF
_
MEMOR
Out of memory
@item CR
_
UNKNOWN
_
ERROR
An unknown error occured
@end table
@subsubheading Example
The following example explains how to send the data in chunks to text
column:
@example
MYSQL
_
BIND bind[1];
long length;
query= "INSERT INTO test
_
long
_
data(text
_
column) VALUES(?)");
if (!mysql
_
prepare(
&
mysql, query, strlen(query))
@
{
fprintf(stderr, "
\n
prepare failed");
fprintf(stderr, "
\n
%s", mysql_error(&stmt));
exit(0);
@
}
memset(bind, 0, sizeof(bind));
bind[0].buffer
_
type= MYSQL
_
TYPE
_
STRING;
bind[0].length=
&
length;
bind[0].is
_
null= 0;
/* Bind the buffers */
if (mysql
_
bind
_
param(stmt, bind))
@
{
fprintf(stderr, "
\n
param bind failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
/* Supply data in chunks to server */
if (!mysql
_
send
_
long
_
data(stmt,1,"MySQL",5))
@
{
fprintf(stderr, "
\n
send
_
long
_
data failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
/* Supply the next piece of data */
if (mysql
_
send
_
long
_
data(stmt,1," - The most popular open source database",40))
@
{
fprintf(stderr, "
\n
send
_
long
_
data failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
/* Now, execute the query */
if (mysql
_
execute(stmt))
@
{
fprintf(stderr, "
\n
mysql
_
execute failed");
fprintf(stderr, "
\n
%s", mysql_stmt_error(stmt));
exit(0);
@
}
This inserts the data, "MySQL - The most popular open source database"
to the field 'text
_
column'.
@end example
@node mysql
_
stmt
_
close, mysql
_
stmt
_
errno, mysql
_
send
_
long
_
data, C Prepared statement functions
@subsubsection @code
{
mysql
_
stmt
_
close()
}
@findex @code
{
mysql
_
stmt
_
close()
}
@code
{
my
_
bool mysql
_
stmt
_
close(MYSQL
_
STMT *)
}
@subsubheading Description
Closes the prepared statement. @code
{
mysql
_
stmt
_
close()
}
also
deallocates the statement handle pointed to by @code
{
stmt
}
.
If the current query results are pending or un-read; this cancels the
query results; so that next call can be executed.
@subsubheading Return Values
Zero if the statement was freed successfully. Non-zero if an error occured.
@subsubheading Errors
@table @code
@item CR
_
SERVER
_
GONE
_
ERROR
The MySQL server has gone away
@item CR
_
UNKNOWN
_
ERROR
An unkown error occured
@end table
@subsubheading Example
For the usage of @code
{
mysql
_
stmt
_
close()
}
refer to the Example from
@ref
{
mysql
_
execute,mysql
_
execute()
}
.
@node mysql
_
stmt
_
errno, mysql
_
stmt
_
error, mysql
_
stmt
_
close, C Prepared statement functions
@subsubsection @code
{
mysql
_
stmt
_
errno()
}
@findex @code
{
mysql
_
stmt
_
errno()
}
@code
{
unsigned int mysql
_
stmt
_
errno(MYSQL
_
STMT *stmt)
}
@subsubheading Description
For the statement specified by @code
{
stmt
}
, @code
{
mysql
_
stmt
_
errno()
}
returns the error code for the most recently invoked statement API
function that can succeed or fail. A return value of zero means that no
error occured. Client error message numbers are listed in the MySQL
errmsg.h header file. Server error message numbers are listed in
mysqld
_
error.h. In the MySQL source distribution you can find a complete
list of error messages and error numbers in the file Docs/mysqld
_
error.txt
@subsubheading Return Values
An error code value. Zero if no error occured.
@subsubheading Errors
None
@node mysql
_
stmt
_
error, mysql
_
commit, mysql
_
stmt
_
errno, C Prepared statement functions
@subsubsection @code
{
mysql
_
stmt
_
error()
}
@findex @code
{
mysql
_
stmt
_
error()
}
.
@code
{
char *mysql
_
stmt
_
error(MYSQL
_
STMT *stmt)
}
@subsubheading Description
For the statement specified by @code
{
stmt
}
, @code
{
mysql
_
stmt
_
error()
}
returns the error message for the most recently invoked statement API
that can succeed or fail. An empty string ("") is returned if no error
occured. This means the following two sets are equivalent:
@example
if (mysql
_
stmt
_
errno(stmt))
@
{
// an error occured
@
}
if (mysql
_
stmt
_
error(stmt))
@
{
// an error occured
@
}
@end example
The language of the client error messages many be changed by recompiling
the MySQL client library. Currently you can choose error messages in
several different languages.
@subsubheading Return Values
A character string that describes the error. An empry string if no error
occured.
@subsubheading Errors
None
@node mysql
_
commit, mysql
_
rollback, mysql
_
stmt
_
error, C Prepared statement functions
@subsubsection @code
{
mysql
_
commit()
}
@findex @code
{
mysql
_
commit()
}
.
@code
{
my
_
bool mysql
_
commit(MYSQL *mysql)
}
@subsubheading Description
Commits the current transaction
@subsubheading Return Values
Zero if successful. Non-zero if an error occured.
@subsubheading Errors
None
@node mysql
_
rollback, mysql
_
autocommit, mysql
_
commit, C Prepared statement functions
@subsubsection @code
{
mysql
_
rollback()
}
@findex @code
{
mysql
_
rollback()
}
.
@code
{
my
_
bool mysql
_
rollback(MYSQL *mysql)
}
@subsubheading Description
Rollbacks the current transaction.
@subsubheading Return Values
Zero if successful. Non-zero if an error occured.
@subsubheading Errors
None.
@node mysql
_
autocommit, mysql
_
more
_
results, mysql
_
rollback, C Prepared statement functions
@subsubsection @code
{
mysql
_
autocommit()
}
@findex @code
{
mysql
_
autocommit()
}
.
@code
{
my
_
bool mysql
_
autocommit(MYSQL *mysql, my
_
bool mode)
}
@subsubheading Description
Sets the autocommit mode to on or off. If the @code
{
mode
}
is '1', then it
sets the autocommit mode to on, else to off in case of '0'.
@subsubheading Return Values
Zero if successful. Non-zero if an error occured
@subsubheading Errors
None.
@node mysql
_
more
_
results, mysql
_
next
_
result, mysql
_
autocommit, C Prepared statement functions
@subsubsection @code
{
mysql
_
more
_
results()
}
@findex @code
{
mysql
_
more
_
results()
}
.
@code
{
my
_
bool mysql
_
more
_
results(MYSQL *mysql)
}
@subsubheading Description
Returns true if more results exists from the currently executed query,
and the application must call @code
{
mysql
_
next
_
result()
}
to fetch the
results.
@subsubheading Return Values
@code
{
TRUE
}
if more results exists. @code
{
FALSE
}
if no more results exists.
@subsubheading Errors
None.
@node mysql
_
next
_
result, , mysql
_
more
_
results, C Prepared statement functions
@subsubsection @code
{
mysql
_
next
_
result()
}
@findex @code
{
mysql
_
next
_
result()
}
.
@code
{
int mysql
_
next
_
result(MYSQL *mysql)
}
@subsubheading Description
If more query results exists, then @code
{
mysql
_
next
_
result()
}
reads the
next query results and returns the status back to application.
@subsubheading Return Values
Zero if successful. Non-zero if an error occured
@subsubheading Errors
None.
@node multiple queries, date handling, C Prepared statement functions, MySQL prepared statements
@subsection Handling multiple query executions
From version 4.1 and above, MySQL supports the multi query execution
using the single command. In order to do this, you must set the client flag
@code
{
CLIENT
_
MULTI
_
QUERIES
}
option during the connection.
@sp 1
By default @code
{
mysql
_
query()
}
or @code
{
mysql
_
real
_
query()
}
returns
only the first query status and the subsequent queries status can
be processed using @code
{
mysql
_
more
_
results()
}
and
@code
{
mysql
_
next
_
result()
}
.
@example
/* Connect to server with option CLIENT
_
MULTI
_
QUERIES */
mysql
_
real
_
query(..., CLIENT
_
MULTI
_
QUERIES);
/* Now execute multiple queries */
mysql
_
query(mysql,"DROP TABLE IF EXISTS test
_
table;
\
CREATE TABLE test
_
table(id int);
\
INSERT INTO test
_
table VALUES(10);
\
UPDATE test
_
table SET id=20 WHERE id=10;
\
SELECT * FROM test
_
table;
\
DROP TABLE test
_
table";
while (mysql
_
more
_
results(mysql))
{
/* Process all results */
mysql
_
next
_
result(mysql);
...
printf("total affected rows:
%lld", mysql_affected_rows(mysql));
...
if ((result= mysql
_
store
_
result(mysql))
{
/* Returned a result set, process it */
}
}
@end example
@node date handling, , multiple queries, MySQL prepared statements
@subsection Handling DATE, TIME and TIMESTAMP
Using the new binary protocol from MySQL 4.1 and above, one can send and
receive the DATE, TIME and TIMESTAMP data using the @code
{
MYSQL
_
TIME
}
structure.
@code
{
MYSQL
_
TIME
}
structure consites of the following members:
@itemize @bullet
@item year
@item month
@item day
@item hour
@item minute
@item second
@item second
_
part
@end itemize
In order to send the data, one must use the prepared statements through
@code
{
mysql
_
prepare()
}
and @code
{
mysql
_
execute()
}
; and must bind the
parameter using type as @code
{
MYSQL
_
TYPE
_
DATE
}
inorder to process date
value, @code
{
MYSQL
_
TYPE
_
TIME
}
for time and @code
{
MYSQL
_
TYPE
_
DATETIME
}
or
@code
{
MYSQL
_
TYPE
_
TIMESTAMP
}
for datetime/timestamp using
@code
{
mysql
_
bind
_
param()
}
when sending and @code
{
mysql
_
bind
_
results()
}
while receiving the data.
@sp 1
Here is a simple example; which inserts the DATE, TIME and TIMESTAMP data.
@example
MYSQL
_
TIME ts;
MYSQL
_
BIND bind[3];
MYSQL
_
STMT *stmt;
strmov(query, "INSERT INTO test
_
table(date
_
field, time
_
field,
timestamp
_
field) VALUES(?,?,?");
stmt= mysql
_
prepare(mysql, query, strlen(query)));
/* setup input buffers for all 3 parameters */
bind[0].buffer
_
type= MYSQL
_
TYPE
_
DATE;
bind[0].buffer= (char *)
&
ts;
bind[0].is
_
null= 0;
bind[0].length= 0;
..
bind[1]= bind[2]= bind[0];
..
mysql
_
bind
_
param(stmt, bind);
/* supply the data to be sent is the ts structure */
ts.year= 2002;
ts.month= 02;
ts.day= 03;
ts.hour= 10;
ts.minute= 45;
ts.second= 20;
mysql
_
execute(stmt);
..
@end example
@bye
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment