📄 fetch.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 - Fetching result set</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="0" 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><!--class="GlbDefault"-->
<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/green.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/red.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>Fetching result set</h1>
</center>
<p>To execute a command and process a result set you should do the following:</p>
<OL>
<LI>Create a command object and set a
command text.
<LI>Bind input parameters (if needed).
<LI>Execute the command.
<LI>Process the result set.</LI></OL>
<p>For example, let's fetch rows from the table
<EM>EMPLOYEES ( NAME CHAR(25), AGE
INTEGER)</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,
"select name, age from employees where
age>:1");</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. This command will select all
rows from the table <EM>EMPLOYEES</EM> where the value of field
<EM>AGE</EM> more than some input value.</p>
<H4>Step 2. Binding input parameters.</H4>
<p>To associate a value with input variable use
<A
href="../OnLineDoc/Command_operator_bind.html">SACommand::operator<<</A>:</p>
<p><FONT face=System>cmd <<
25;</FONT></p>
<p>This line sets the input parameter marked by
<EM>:1</EM> to value <EM>25</EM>.</p>
<p>The Library provides several ways for
binding input variables. To get more details see <A
href="bind.html">Binding input parameters</A>.</p>
<H4>Step 3. Execute the command.</H4>
<P>To execute a command use <A
href="../OnLineDoc/Command_Execute.html">SACommand::Execute</A>
method:</P>
<P><FONT face=System>cmd.Execute();</FONT></P>
<P>This command selects all rows from the table
where field <EM>AGE</EM> value more than <EM>25</EM>. The result of
command execution is the <EM>result set</EM> - a set of rows
corresponding with the request. </P>
<P>To check whether a result set exists after
the command execution use <A
href="../OnLineDoc/Command_isResultSet.html">SACommand::isResultSet</A>
method:</P>
<P><FONT face=System>bool is_result =
cmd.isResultSet(); <BR></FONT></P>
<H4>Step 4. Processing the result set.</H4>
<P>After the command execution a set of <A
href="../OnLineDoc/Field.html">SAField</A>objects is created
implicitly. Each <A href="../OnLineDoc/Field.html">SAField</A>
object represents a column in the result set. To get the number of
columns in the result set call <A
href="../OnLineDoc/Command_FieldCount.html">SACommand::FieldCount</A>
method:</P>
<P><FONT face=System>long col_count =
cmd.FieldCount();</FONT></P>
<P>Each field can be reached by its name or
position. To get a field information (name, type, size, etc)
directly after the command execution you can call <A
href="../OnLineDoc/Command_Field.html">SACommand::Field</A>
method:</P>
<P><FONT face=System>SAField& Field =
cmd.Field("age");</FONT></P>
<P>The line above associates the column
<EM>AGE</EM> in the result set with <EM>Field</EM> variable. We can
do the same getting column <EM>AGE</EM> by its position in the
result set:</P>
<P><FONT face=System>SAField& Field =
cmd.Field(2);</FONT></P>
<P>A field value is updated only after row
fetching. To fetch row by row from the result set use <A
href="../OnLineDoc/Command_FetchNext.html">SACommand::FetchNext</A>
method:</P>
<P><FONT
face=System>while(cmd.FetchNext())<BR>
printf("Name: %s, age: %d \n",
<BR>
cmd.Field("name").asString(),
<BR>
cmd.Field("age").asLong()); </FONT></P>
<P>In example above we reached a field value
accessing appropriated <A href="../OnLineDoc/Field.html">SAField</A>
object. Another way to get a field value is to use <A
href="../OnLineDoc/Command_operator_get.html">SACommand::operator[]</A>:</P>
<P><FONT
face=System>while(cmd.FetchNext())<BR>
printf("Name: %s, age: %d \n",
<BR>
cmd[1].asString(),
<BR>
cmd[2].asLong()); </FONT></P>
<P><BR>
<P>You can use <A
href="../OnLineDoc/Field.html"><STRONG>SAField</A>
operators</STRONG> for quick accessing values. In this case you can
organize a fetching process as shown below:</P>
<P><FONT
face=System>while(cmd.FetchNext())<BR>{<BR> SAString sName =
cmd.Field("name");<BR> long nAge =
cmd.Field("age");<BR> printf("Name:
%s, age: %d \n", sName, nAge);<BR>
}
<BR>
</FONT><BR>or another variant:</P>
<P><FONT
face=System>while(cmd.FetchNext())<BR>{<BR> SAString sName =
cmd[1];<BR> long nAge =
cmd[2];<BR> printf("Name: %s, age: %d
\n", sName, nAge);<BR>
}
<BR>
</FONT></P>
<P>Processing Long, BLob and CLob data can
have some differences from processing other data types. <FONT
color=black face="">See <A href="blobs.html" >Working with Long or
Lob(CLob, BLob) Data</A> to get more information.</FONT></P>
<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>
</td>
<tr></tr>
</table></FONT>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -