<s>
In	O
database	B-Application
management	I-Application
systems	I-Application
(	O
DBMS	B-General_Concept
)	O
,	O
a	O
prepared	B-Language
statement	I-Language
,	O
parameterized	B-Language
statement	I-Language
,	O
or	O
parameterized	B-Language
query	I-Language
is	O
a	O
feature	O
used	O
to	O
pre-compile	O
SQL	B-Language
code	I-Language
,	O
separating	O
it	O
from	O
data	O
.	O
</s>
<s>
Benefits	O
of	O
prepared	B-Language
statements	I-Language
are	O
:	O
</s>
<s>
A	O
prepared	B-Language
statement	I-Language
takes	O
the	O
form	O
of	O
a	O
pre-compiled	O
template	B-Language
into	O
which	O
constant	O
values	O
are	O
substituted	O
during	O
each	O
execution	O
,	O
and	O
typically	O
use	O
SQL	B-Language
DML	I-Language
statements	O
such	O
as	O
INSERT	B-Language
,	O
SELECT	B-Language
,	O
or	O
UPDATE	B-Language
.	O
</s>
<s>
A	O
common	O
workflow	O
for	O
prepared	B-Language
statements	I-Language
is	O
:	O
</s>
<s>
Prepare	O
:	O
The	O
application	O
creates	O
the	O
statement	O
template	B-Language
and	O
sends	O
it	O
to	O
the	O
DBMS	B-General_Concept
.	O
</s>
<s>
Certain	O
values	O
are	O
left	O
unspecified	O
,	O
called	O
parameters	O
,	O
placeholders	O
or	O
bind	B-Language
variables	I-Language
(	O
labelled	O
"	O
?	O
"	O
</s>
<s>
Compile	O
:	O
The	O
DBMS	B-General_Concept
compiles	O
(	O
parses	O
,	O
optimizes	B-Language
and	O
translates	O
)	O
the	O
statement	O
template	B-Language
,	O
and	O
stores	O
the	O
result	O
without	O
executing	O
it	O
.	O
</s>
<s>
Execute	O
:	O
The	O
application	O
supplies	O
(	O
or	O
binds	O
)	O
values	O
for	O
the	O
parameters	O
of	O
the	O
statement	O
template	B-Language
,	O
and	O
the	O
DBMS	B-General_Concept
executes	O
the	O
statement	O
(	O
possibly	O
returning	O
a	O
result	O
)	O
.	O
</s>
<s>
The	O
application	O
may	O
request	O
the	O
DBMS	B-General_Concept
to	O
execute	O
the	O
statement	O
many	O
times	O
with	O
different	O
values	O
.	O
</s>
<s>
The	O
alternative	O
to	O
a	O
prepared	B-Language
statement	I-Language
is	O
calling	O
SQL	B-Language
directly	O
from	O
the	O
application	O
source	O
code	O
in	O
a	O
way	O
that	O
combines	O
code	O
and	O
data	O
.	O
</s>
<s>
Not	O
all	O
optimization	O
can	O
be	O
performed	O
at	O
the	O
time	O
the	O
statement	O
template	B-Language
is	O
compiled	O
,	O
for	O
two	O
reasons	O
:	O
the	O
best	O
plan	O
may	O
depend	O
on	O
the	O
specific	O
values	O
of	O
the	O
parameters	O
,	O
and	O
the	O
best	O
plan	O
may	O
change	O
as	O
tables	O
and	O
indexes	O
change	O
over	O
time	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
if	O
a	O
query	O
is	O
executed	O
only	O
once	O
,	O
server-side	O
prepared	B-Language
statements	I-Language
can	O
be	O
slower	O
because	O
of	O
the	O
additional	O
round-trip	O
to	O
the	O
server	O
.	O
</s>
<s>
Implementation	O
limitations	O
may	O
also	O
lead	O
to	O
performance	O
penalties	O
;	O
for	O
example	O
,	O
some	O
versions	O
of	O
MySQL	B-Application
did	O
not	O
cache	O
results	O
of	O
prepared	B-Language
queries	I-Language
.	O
</s>
<s>
A	O
stored	B-General_Concept
procedure	I-General_Concept
,	O
which	O
is	O
also	O
precompiled	O
and	O
stored	O
on	O
the	O
server	O
for	O
later	O
execution	O
,	O
has	O
similar	O
advantages	O
.	O
</s>
<s>
Unlike	O
a	O
stored	B-General_Concept
procedure	I-General_Concept
,	O
a	O
prepared	B-Language
statement	I-Language
is	O
not	O
normally	O
written	O
in	O
a	O
procedural	O
language	O
and	O
cannot	O
use	O
or	O
modify	O
variables	O
or	O
use	O
control	O
flow	O
structures	O
,	O
relying	O
instead	O
on	O
the	O
declarative	O
database	B-Application
query	I-Application
language	O
.	O
</s>
<s>
Due	O
to	O
their	O
simplicity	O
and	O
client-side	O
emulation	O
,	O
prepared	B-Language
statements	I-Language
are	O
more	O
portable	O
across	O
vendors	O
.	O
</s>
<s>
Major	O
DBMSs	O
,	O
including	O
SQLite	B-Language
,	O
MySQL	B-Application
,	O
Oracle	B-General_Concept
,	O
IBM	B-Application
Db2	I-Application
,	O
Microsoft	B-Application
SQL	I-Application
Server	I-Application
and	O
PostgreSQL	B-Application
support	O
prepared	B-Language
statements	I-Language
.	O
</s>
<s>
Prepared	B-Language
statements	I-Language
are	O
normally	O
executed	O
through	O
a	O
non-SQL	O
binary	O
protocol	O
for	O
efficiency	O
and	O
protection	O
from	O
SQL	B-Language
injection	I-Language
,	O
but	O
with	O
some	O
DBMSs	O
such	O
as	O
MySQL	B-Application
prepared	B-Language
statements	I-Language
are	O
also	O
available	O
using	O
a	O
SQL	B-Language
syntax	O
for	O
debugging	O
purposes	O
.	O
</s>
<s>
A	O
number	O
of	O
programming	O
languages	O
support	O
prepared	B-Language
statements	I-Language
in	O
their	O
standard	O
libraries	O
and	O
will	O
emulate	O
them	O
on	O
the	O
client	O
side	O
even	O
if	O
the	O
underlying	O
DBMS	B-General_Concept
does	O
not	O
support	O
them	O
,	O
including	O
Java	B-Language
's	O
JDBC	B-Language
,	O
Perl	B-Language
's	O
DBI	B-Language
,	O
PHP	B-Application
's	O
PDO	O
and	O
Python	B-Language
's	O
DB-API	O
.	O
</s>
<s>
It	O
resists	O
SQL	B-Language
injection	I-Language
attacks	I-Language
equally	O
effectively	O
.	O
</s>
<s>
Many	O
types	O
of	O
SQL	B-Language
injection	I-Language
attacks	I-Language
can	O
be	O
eliminated	O
by	O
disabling	O
literals	O
,	O
effectively	O
requiring	O
the	O
use	O
of	O
prepared	B-Language
statements	I-Language
;	O
only	O
H2	B-Language
supports	O
this	O
feature	O
.	O
</s>
<s>
This	O
example	O
uses	O
Java	B-Language
and	O
JDBC	B-Language
:	O
</s>
<s>
Java	B-Language
PreparedStatement	O
provides	O
"	O
setters	O
"	O
(setInt(int )	O
,	O
setString(String )	O
,	O
setDouble(double )	O
,	O
etc	O
.	O
)	O
</s>
<s>
This	O
example	O
uses	O
PHP	B-Application
and	O
PDO	O
:	O
</s>
<s>
This	O
example	O
uses	O
Perl	B-Language
and	O
DBI	B-Language
:	O
</s>
<s>
This	O
example	O
uses	O
C#	B-Application
and	O
ADO.NET	B-Device
:	O
</s>
<s>
ADO.NET	B-Device
SqlCommand	O
will	O
accept	O
any	O
type	O
for	O
the	O
value	O
parameter	O
of	O
AddWithValue	O
,	O
and	O
type	O
conversion	O
occurs	O
automatically	O
.	O
</s>
<s>
This	O
example	O
uses	O
Python	B-Language
and	O
DB-API	O
:	O
</s>
<s>
SQL	B-Language
Command	O
:	O
</s>
<s>
For	O
SQLite	B-Language
,	O
ODBC	O
,	O
MariaDB/Mysql	O
use	O
:	O
?	O
</s>
<s>
For	O
PostgreSQL	B-Application
use	O
:	O
$1	O
,	O
$2	O
,	O
$3	O
,	O
...	O
</s>
