📄 command_examples.html
字号:
<A name=FetchNext></A>
<H3>FetchNext</H3>
<FONT face=Courier>
<P>
/*<BR>
Fetch result set
row by row.<BR>
Exception handling is omitted
for simplicity.<BR>
*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P>SACommand cmd(&con, "Select * from
test_tbl");</P>
<P>cmd.Execute();</P>
<P>while(<STRONG>cmd.FetchNext()</STRONG>)<BR>{<BR> ...<BR>}
</FONT></P>
<P>
<HR>
<A name=CreateParam></A>
<P></P>
<H3>CreateParam</H3>
<FONT face=Courier>
<P>
/*<BR>
Create params
explicitly.<BR>
Exception handling is omitted
for simplicity.<BR>
*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P> SACommand cmd(&con,
"test_proc");</P>
<P><STRONG>cmd.CreateParam("nInput", SA_dtLong, sizeof(long),
SA_ParamInput);</STRONG> </P>
<P><STRONG><STRONG>cmd.CreateParam("nOutput", SA_dtLong,
sizeof(long), SA_ParamOutput);</STRONG></STRONG> </P>
<P>cmd << SAPos("nInput")
<< 1L; /*bind input*/</P>
<P> cmd.Execute();</P>
<P>printf("Output value: %ld\n",
cmd.Param("nOutput").asLong());</P>
</FONT>
<HR>
<A name=DestroyParams></A>
<H3>DestroyParams</H3>
<FONT face=Courier>
<P>
/*<BR>
Destroy params
explicitly.<BR>> Exception handling is
omitted for simplicity.<BR>
*/</P>
<P>
SAConnection con;</P>
<P>SACommand cmd(&con,
"test_proc");</P>
<P>...</P>
<P><STRONG>cmd.DestroyParams();</STRONG></P>
</FONT>
<HR>
<A name=ParamCount></A>
<H3>ParamCount</H3>
<FONT face=Courier>
<P>
/*<BR>
Print procedure
parameters names.<BR>
Exception handling is
omitted for simplicity.<BR>
*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P>SACommand cmd(&con,
"test_proc");</P>
<P>for(int i = 0; i < <STRONG>cmd.ParamCount();
</STRONG>++i)<BR>{<BR> printf("Param %d name:
%s\n", i+1, (const char *)cmd.ParamByIndex().Name());<BR>}</P>
</FONT>
<HR>
<A name=ParamByIndex></A>
<H3>ParamByIndex</H3>
<FONT face=Courier>
<P>
<FONT face=Courier>
/*<BR>
Print procedure parameters
names.<BR>
Exception handling is omitted for
simplicity.<BR>
*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P>SACommand cmd(&con,
"test_proc");</P>
<P>for(int i = 0; i < cmd.ParamCount();
++i)<BR>{<BR> printf("Param %d name: %s\n",
i+1, (const char *)<STRONG>cmd.ParamByIndex(i)</STRONG>.Name());<BR>}</P>
</FONT></FONT>
<HR>
<A name=Param></A>
<H3>Param</H3>
<FONT face=Courier>
<P>
/*<BR>
Bind parameters
by name and position.<BR>
Exception handling is
omitted for simplicity.<BR>
*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P>SACommand cmd(&con, "Select * from
test_tbl where fid = :1 or fid = :fidvalue2");</P>
<P><STRONG>cmd.Param(1)</STRONG>.setAsLong() = 15;</P>
<P><STRONG>cmd.Param("fidvalue2")</STRONG>.setAsLong() =
32;</P>
<P> cmd.Execute();</P>
<P> ...</P>
</FONT>
<HR>
<A name=operator_bind></A>
<H3>operator <<</H3>
<FONT face=Courier>
<FONT face=Courier>
<P>
/*<BR>
Bind parameters
by name and position.<BR>
Exception handling is
omitted for simplicity.<BR>
*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P>SACommand cmd(&con, "Select * from
test_tbl where fid = :1 or fid = :fidvalue2");</P>
<P><STRONG>cmd <<
15L << SAPos("fidvalue2") << 32L;</STRONG>
</P>
<P> cmd.Execute();</P>
<P> ...</P>
</FONT>
</FONT>
<P></P>
<HR>
<A name=FieldCount></A>
<H3>FieldCount</H3>
<FONT face=Courier>
<P><FONT face=Courier>
</P>
<P>/*<BR>Print select
statement fields names.<BR>Exception handling
is omitted for simplicity.<BR>*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P>SACommand cmd(&con, "Select * from
test_tbl");</P>
<P>cmd.Execute();</P>
<P>for(int i = 0; i < <STRONG>cmd.FieldCount();
</STRONG>++i)<BR>{<BR> printf("Field %d name:
%s\n", i+1, (const char *)cmd.Field(i+1).Name());<BR>}</P>
</FONT></FONT>
<HR>
<A name=Field></A>
<H3>Field</H3>
<FONT face=Courier>
<P>/*<BR>Exception
handling is omitted for simplicity.<BR>*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P>SACommand cmd(&con, "Select fid
from test_tbl");</P>
<P>cmd.Execute();</P>
<P>if(cmd.FetchNext())<BR>{<BR> if(<STRONG>cmd.Field(1)</STRONG>.asLong() !=
<STRONG>cmd.Field("fid")</STRONG>.asLong())<BR> printf("Very
strange!\n");<BR>}</P>
<P>
</FONT>
<HR>
<A name="operator_get"></A>
<P></P>
<H3>operator []</H3>
<FONT face=Courier>
<P><FONT face=Courier>/*<BR>Exception handling is omitted for
simplicity.<BR>
*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P>SACommand cmd(&con, "Select fid
from test_tbl");</P>
<P>cmd.Execute();</P>
<P>if(cmd.FetchNext())<BR>{<BR> if(<STRONG>cmd[1]</STRONG>.asLong() !=
<STRONG>cmd["fid"]</STRONG>.asLong())<BR>
printf("Very strange!\n");<BR>}</P>
<P>
</FONT></FONT>
<HR>
<A name=NativeHandles></A>
<P></P>
<H3>NativeHandles</H3>
<P><FONT face=Courier>/*<BR>Exception handling is omitted for
simplicity.<BR>*/</FONT><FONT face=Courier></FONT></P><FONT
face=Courier><FONT face=Courier>
<P>#include <ora7API.h><BR>#include <oraAPI.h></P>
<P>SAConnection con;</FONT></P>
<P>con.Connect("dbname",
"username", "password", SA_Oracle_Client);</P>
<P>SACommand cmd(&con, "Select * from
test_tbl");</P>
<P>cmd.Open(); /* make sure command handles are
initialized*/</P>
<P>if(con.ClientVersion() >= 0x0008000) /*
OCI 8.x */<BR>{</P>
<P> ora8API *p_ora8API =
(ora8API *)con.NativeAPI();</P>
<P> ora8CommandHandles
*p_ora8CH = (ora8CommandHandles *)cmd.NativeHandles();</P>
<P> /* call any OCI*
function(s) */<BR> /* passing
required command handles */<BR> /* f.ex., p_ora8CH->m_pOCIStmt */<BR> ...</P>
<P>}<BR>else /* OCI 7.x
*/<BR>{</P>
<P> ora7API *p_ora7API =
(ora7API *)con.NativeAPI();</P>
<P> ora7CommandHandles
*p_ora7CH = (ora7CommandHandles *)cmd.NativeHandles();</P>
<P> /* call any o*
function(s) */<BR> /* passing
required command handles */<BR> /* f.ex., p_ora7CH->m_cda */<BR> ...</P>
<P>}</FONT></P>
<HR>
<H3><A name=Option>Option</A></H3>
<FONT face=Courier><FONT face=Courier>
<P>
/*<BR>
Example 1.
InterBase server option SQL Dialect.<BR>Exception handling is omitted for simplicity.<BR>
*/
</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password",
SA_InterBase_Client);</P>
<P> /* assume t_field is a TIMESTAMP type field
(introduced in SQL Dialect 3)<BR>*/<BR>SACommand cmd(&con, "Select t_field
from test_tbl");</P>
<P><STRONG>cmd.setOption("SQLDialect")</STRONG>=
"1";</P>
<P>cmd.Execute(); // returns an error<BR>
// because there is no TIMESTAMP
<BR>
// data type in SQL Dialect 1.</P>
<P><STRONG>cmd.setOption("SQLDialect")</STRONG>="3";</P>
<P> cmd.setCommandText("Select t_field from
test_tbl");</P>
<P>cmd.Execute(); // no error</P>
<P>while(cmd.FetchNext())<BR>{<BR> ...<BR>}</P>
<FONT face=Courier><FONT face=Courier>
<P>
/*<BR>
Example 2.
SQLBase server option SQLPPCX (cursor-context preservation).<BR>Exception handling is omitted for
simplicity.<BR>
*/
</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password", SA_SQLBase_Client);</P>
<P> SACommand cmd(&con, "Select fid
from test_tbl");</P><STRONG>
<P>cmd.setOption("SQLPPCX")</STRONG>="on";</P>
<P>cmd.Execute();</P>
<P>con.Commit(); // result set will not be
destroyed<BR>
// after that command because SQLPPCX<BR>
// option is "on" </P>
<P>while(cmd.FetchNext())<BR>{ <BR> printf("%d\n",
cmd[1].asLong());<BR>}</P>
<P><STRONG>cmd.setOption("SQLPPCX")</STRONG>="off";</P>
<P> cmd.setCommandText("Select t_field from
test_tbl");</P>
<P>cmd.Execute();</P>
<P>con.Commit(); // result set will be
destroyed<BR>
// after that command because SQPPCX<BR>
// option is "off" </P>
<FONT face=Courier><FONT face=Courier>
<P>
/*<BR>
Example 3.
SQLServer command option OpenCursor.<BR>Exception handling is omitted for simplicity.<BR>
*/
</P>
<P>/*The following code of Example 3 is
incorrect,<BR>it cause an error. See the right code below.*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password",
SA_SQLServer_Client);</P>
<P> SACommand cmd1(&con, "Select fid
from test_tbl");</P>
<P> SACommand cmd2(&con, "Insert into
temp_tbl (fid_tmp) values (:1)");</P>
<P>cmd1.Execute();</P>
<P>while(cmd1.FetchNext())<BR>{ <BR> cmd2 <<
cmd1[1].asLong();<BR> cmd2.Execute(); // returns
an
error<BR>
// "Attempt to
initiate<BR>
// a new SQLServer operation
with<BR>
// results pending"<BR>}<BR>/* end of
incorrect code */</P>
<P>/*The following code of Example 3 is
correct.*/</P>
<P>SAConnection con;</P>
<P>con.Connect("dbname",
"username", "password",
SA_SQLServer_Client);</P>
<P> SACommand cmd1(&con, "Select fid
from test_tbl");</P>
<P><STRONG>cmd1.setOption("OpenCursor")</STRONG>="100";</P>
<P> SACommand cmd2(&con, "Insert into
temp_tbl (fid_tmp) values (:1)");</P>
<P>cmd1.Execute();<BR></P>
<P>while(cmd1.FetchNext())<BR>{ <BR> cmd2 <<
cmd1[1].asLong();<BR> cmd2.Execute(); // no
error<BR>}<BR></P>
<P></P>
<P>con.Commit(); </FONT>
</FONT></P></FONT>
</FONT></FONT>
</FONT>
<HR>
<h3>Problems and Questions</h3>
<p> If you haven't found the answer to your
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>
</td>
<tr></tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -