📄 ex12_8c-example.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>libsqlora8: Example Documentation</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a> | <a class="qindex" href="examples.html">Examples</a></div><h1>ex12.c</h1>Example for fetching data into an array of structures. <div class="fragment"><pre><span class="comment">/* $Id: ex12.c,v 1.4 2002/08/24 12:54:47 kpoitschke Exp $ */</span><span class="preprocessor">#include <stdio.h></span><span class="preprocessor">#include <stdlib.h></span><span class="preprocessor">#include "examples.h"</span><span class="keyword">enum</span> { MAX_NAME_LEN = 64, MAX_ARRAY_SIZE = 4 <span class="comment">/* Note: usually this should be 100 or so. </span><span class="comment"> * Used a smaller one to do test all paths in the code</span><span class="comment"> */</span>};<span class="keyword">typedef</span> <span class="keyword">struct </span>_result_t { <span class="keywordtype">char</span> name[MAX_NAME_LEN+1]; <span class="comment">/* output ENAME */</span> <span class="keywordtype">double</span> salary; <span class="comment">/* output SAL */</span> <span class="keywordtype">short</span> name_ind; <span class="comment">/* indicator variable of ENAME */</span> <span class="keywordtype">short</span> sal_ind; <span class="comment">/* indicator variable of SAL */</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> name_rlen; <span class="comment">/* the actual length of ENAME */</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> name_rcode; <span class="comment">/* the return code of ENAME */</span>} result_t;<span class="keywordtype">void</span> print_record __P((result_t * r));<span class="keywordtype">int</span> do_array_select(<a class="code" href="group__typedefs.html#ga0">sqlo_db_handle_t</a> dbh, <span class="keywordtype">double</span> min_salary){ <a class="code" href="group__typedefs.html#ga1">sqlo_stmt_handle_t</a> sth; <span class="comment">/* statement handle */</span> <span class="keywordtype">int</span> i; <span class="comment">/* loop variable */</span> <span class="keywordtype">int</span> status; <span class="comment">/* return code of sqlo_... */</span> result_t result[MAX_ARRAY_SIZE]; <span class="keywordtype">int</span> rows_fetched; <span class="comment">/* number of rows fetched per execute */</span> <span class="keywordtype">int</span> rows_fetched_total = 0; <span class="comment">/* total number of rows */</span> <span class="keywordtype">int</span> done_fetching = 0; <span class="comment">/* flag indicating end of fetch */</span> <span class="keywordtype">double</span> salary = min_salary; <span class="comment">/* input variable for SAL */</span> <span class="keywordtype">int</span> skip_size = <span class="keyword">sizeof</span>(result[0]); <span class="comment">/* The skip size */</span> sth = prepare_cursor(dbh, &salary); <span class="comment">/* see ex10.c */</span> <span class="comment">/* define output */</span> <span class="keywordflow">if</span> (<a class="code" href="group__constants.html#gga3a25">SQLO_SUCCESS</a> != (<a name="a6"></a><a class="code" href="group__complex.html#ga193">sqlo_define_by_pos2</a>(sth, 1, SQLOT_STR, result[0].name, <span class="keyword">sizeof</span>(result[0].name), &result[0].name_ind, &result[0].name_rlen, &result[0].name_rcode, skip_size)) || (<a class="code" href="group__complex.html#ga193">sqlo_define_by_pos2</a>(sth, 2, SQLOT_FLT, &result[0].salary, <span class="keyword">sizeof</span>(result[0].salary), &result[0].sal_ind, 0, 0, skip_size))) { error_exit(dbh, <span class="stringliteral">"sqlo_define_by_pos"</span>); } <span class="comment">/* execute and fetch the result */</span> status = <a name="a7"></a><a class="code" href="group__complex.html#ga194">sqlo_execute</a>(sth, 0); <span class="keywordflow">while</span> (!done_fetching) { rows_fetched = MAX_ARRAY_SIZE; <span class="comment">/* get the next set */</span> status = <a name="a8"></a><a class="code" href="group__easy.html#ga167">sqlo_fetch</a>(sth, MAX_ARRAY_SIZE); <span class="keywordflow">if</span> (0 > status) error_exit(dbh, <span class="stringliteral">"sqlo_execute(NEXT)"</span>); <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="group__constants.html#gga3a32">SQLO_NO_DATA</a> == status) { rows_fetched = <a name="a9"></a><a class="code" href="group__easy.html#ga169">sqlo_prows</a>(sth); <span class="comment">/* sqlo_prows returns now the total number of fetched rows</span><span class="comment"> * the difference to the previous total fechted rows is</span><span class="comment"> * the number of rows fetched in this last call to sqlo_execute</span><span class="comment"> */</span> rows_fetched = rows_fetched - rows_fetched_total; done_fetching = 1; } <span class="comment">/* print the records */</span> <span class="keywordflow">for</span> (i = 0; i < rows_fetched; ++i) print_record(&result[i]); rows_fetched_total += rows_fetched; } printf(<span class="stringliteral">"Selected %d employees\n"</span>, rows_fetched_total); <span class="comment">/* finished. */</span> <a name="a10"></a><a class="code" href="group__easy.html#ga172">sqlo_close</a>(sth); <span class="keywordflow">return</span> 1;}<span class="comment">/* print record */</span><span class="keywordtype">void</span> print_record(result_t * r <span class="comment">/* I - The record */</span> ){ printf(<span class="stringliteral">"Name=%-8s Salary= %6.2f\n"</span>, (r->name_ind == SQLO_NULL_IND ? <span class="stringliteral">"NULL"</span> : r->name), (r->sal_ind == SQLO_NULL_IND ? -1.0 : r->salary));}<span class="comment">/* $Id: ex12.c,v 1.4 2002/08/24 12:54:47 kpoitschke Exp $ */</span></pre></div> <hr size="1"><address style="align: right;"><small>Generated on Sun Jun 13 04:51:41 2004 for libsqlora8 by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.6 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -