📄 commands.html
字号:
<html><head><meta name="KEYWORDS" content=" library, C++, Oracle, SQLServer, DB2, Sybase, Informix, InterBase, SQLBase, ODBC,
SQL, server, client, database, programming, development, software,
Win32, Windows, Linux, Unix, Solaris,
Microsoft, Borland, Centura, Gupta, IBM"><meta name="DESCRIPTION" content=
"SQLAPI++ is C++ library for accessing SQL databases (Oracle, SQLServer, Sybase, DB2, InterBase, SQLBase, Informix, ODBC).
It uses native APIs, provides low-level access to database-specific features."><title>SQLAPI++ Library - How To - Executing an SQL command</title></head><body bgcolor="#ffffff" BACKGROUND="../images/bg.gif" text="#000000"><!--class="GlbHdr"-->
<table WIDTH="600" height="40" frame="hsides" BORDER="2" BORDERCOLOR="#004fcd" align="right" cellpadding=2 cellspacing=2 id="TABLE1">
<tr><!--class="GlbDefault"-->
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="../index.html" style="TEXT-DECORATION: none" >
<font color="white" size="3" face="sans-serif">
<b>Home</b></font></A></td></TD><!--class="GlbActive"-->
<td width="70" bgcolor="#00bb00" bordercolor="black" align="middle"><A href="../HowTo/index.html" style="TEXT-DECORATION: none" >
<font color="white" size="3" face="sans-serif">
<b>How To</b></font></A></td><!--class="GlbDefault"-->
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="../OnLineDoc/index.html" style="TEXT-DECORATION: none" >
<font color="white" size="3" face="sans-serif">
<b>Online Documentation</b></font></A></td>
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="../Support/index.html" style="TEXT-DECORATION: none" >
<font color="white" size="3" face="sans-serif">
<b>Support</b></font></A></td>
<td width=70 bgcolor=#000094 bordercolor=black align=middle>
<A href="../Download/index.html" style="TEXT-DECORATION: none" >
<font color=white size=3
face=sans-serif>
<b>Download</b></font></A></td>
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="../Order/index.html" style="TEXT-DECORATION: none" >
<font color="white" size="3" face="sans-serif">
<b>Order</b></font></A></td>
</tr>
</table>
<p> </p>
<p> </p>
<table width="100%" cellpadding=1 cellspacing=1>
<tr>
<td valign="top" align=left><!--class="LeftMenu"-->
<table frame="void" CELLSPACING=2 cellpadding=2 width=165>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="connect.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Connecting to databases</font></A> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="transactions.html" style ="TEXT-DECORATION: none" >
<font color="white" face="sans-serif" size=2>
Transactions</font></A> </td>
</tr>
<tr>
<td><img align="middle" src="../images/red.gif" WIDTH="12"
HEIGHT="12"
>
<A href="commands.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Executing an SQL command</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="procedures.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Executing stored procedures</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="bind.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Binding input parameters</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="fetch.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Fetching result set</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12"
>
<A href="fetch_multiple.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Fetching multiple result sets</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12"
>
<A href="performance.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Optimizing Database Performance</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="return_param.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Returning output parameters</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="blobs.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Working with
Long or Lob(CLob, BLob) Data</A>
</FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12"> <A href="datetime.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
Working with date/time values</a> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="errors.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Error handling</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="query_exampl.html" style ="TEXT-DECORATION: none" >
<font color="white" size="2" face="sans-serif">
Query examples</A> </FONT> </td>
</tr>
</table>
</td>
<td width="30">
<p> </p>
</td>
<td valign="top">
<center>
<h1>Executing an SQL command</h1>
</center>
<p> To execute an SQL command you should do the following:</p>
<OL>
<LI>Create a command object and set a
command text.
<LI>Bind input parameters.
<LI>Execute the command.
<LI>Processing output parameters and result set.</LI></OL>
<P> In this section we'll consider a commands like INSERT, SELECT
and UPDATE that do not involve fetching data. To execute SELECT
command or a stored procedures see <A href="fetch.html">Fetching
result set</A>
and <A href="procedures.html">Executing stored
procedures</A>
sections.</P>
<P>In the following example we assume that the table <EM>EMPLOYEES (
NAME CHAR(25), AGE INTEGER)</EM> exists on the database
<EM>Demo</EM>. </P>
<H4>Step 1. Creating a command object and setting a command
text.</H4>
<P>To execute a command we need two objects: <A href="../OnLineDoc/Connection.html" >SAConnection</A>
(connection object) and <A href="../OnLineDoc/Command.html" >SACommand</A>
(command object).</P>
<p><STRONG><FONT face=""
size=2><FONT face=System><FONT>SACommand cmd(&Connection,
"insert into employees (name, age) values (:1,
:2)");</FONT></FONT></FONT></STRONG><FONT
><FONT face=System></FONT></FONT> </p>
<p>The line above creates a command object <EM
>cmd</EM> based on previously created and
connected connection object <EM>Connection</EM>
(for creating and connection <EM>Connection</EM>
object see <A href="connect.html" >Connecting to
databases</A> ). Second parameter is the command text.
The command text can also be provided using <A href="../OnLineDoc/Command_setCommandText.html" >SACommand::setCommandText</A>
method.</p>
<p><FONT face=System size=2><STRONG>SACommand cmd(&Connection);<BR
>cmd.setCommandText("insert into employees (name, age)
values (:1, :2)");</STRONG></FONT> </p>
<p><FONT face=System size=2><STRONG></STRONG></FONT> </p>
<H4>Step 2. Bind input parameters.</H4>
<P>Binding some values to the command is like a filling in the gaps
in the SQL command statement (marked by <EM>:1, :2, :3, ...,
:n</EM>) with actual data values. The data values can be constants
or program variables of appropriate types. To associate a command
object with these bind variables you can use the <A
href="../OnLineDoc/Command_operator_bind.html">SACommand::operator<<</A>
as shown below:</P>
<P><FONT face=System>cmd << "Tom Patt" <<
30;</FONT></P>
<P>The order that you associate the values is important (i.e. the
first '<<' associates a bind with <EM>:1</EM>, the second
'<<' with <EM>:2</EM> ). </P>
<P>The another way to bind input variables is directly assigning a
value with <A href="../OnLineDoc/Param.html">SAParam</A>
object which represents a command parameter. To get appropriated
parameter object by its position in SQL statement use <A
href="../OnLineDoc/Command_Param.html">SACommand::Param</A>
method:</P>
<P><FONT face=System>cmd.Param(1).setAsString() = "Tom
Patt";<BR>cmd.Param(2).setAsLong() = 30;</FONT></P>
<P>More details about binding input variables see in <A
href="bind.html">Binding input parameters</A> section.</P>
<P> </P>
<H4>Step 3. Executing the command.</H4>
<P>Finally we execute the command, which results in the string being
inserted into column <EM>NAME</EM>, and the number being inserted
into column <EM>AGE</EM>.</P>
<P><FONT face=System>cmd.Execute();</FONT></P>
<P>If you want to see the number of rows affected by the command
execution use <A
href="../OnLineDoc/Command_RowsAffected.html">SACommand::RowsAffected</A>
method:</P>
<P><FONT face=System>int nRows = cmd.RowsAffected();</FONT></P>
<P>If you want to insert another row of new data, you just have to
associate the new bind values with the object and repeat the
bind/execute part again: </P>
<P><FONT face=System>cmd << "Nick Barry" <<
37;<BR>cmd.Execute();</FONT></P>
<P> </P>
<H4>Step 4. Processing output parameters and result set.</H4>
<P>Processing output parameters and result set
is not actual for INSERT, UPDATE and DELETE commands, that's why we
discuss this questions in other sections.</P>
<P>For processing result set (if any) see <A href="fetch.html" >Fetching result set</A>.</P>
<P>For processing output parameters (if any)
see <A href="return_param.html">Returning output
parameters</A>. </P>
<p>
<table cellPadding=1 cellSpacing=1 width="100%">
<tr>
<td vAlign=top>
<h3>Problems and Questions</h3>
<p>If you don't find the answer to
you questions or have some problems on using the
Library, please, send e-mail to <A href="mailto:howto@sqlapi.com" >howto@sqlapi.com</A>. </p>
<P> </P>
<P> </P></td>
<tr></tr></table></FONT> </p>
</td>
<tr></tr>
</table></FONT>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -