1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
<!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>Simple&nbsp;C++&nbsp;ODBC&nbsp;Database&nbsp;API: complete.cpp</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.2 -->
<div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="pages.html">Related Pages</a> | <a class="qindex" href="examples.html">Examples</a></div>
<h1>complete.cpp</h1>This is an example of how to use the Simple Database API. This example makes use of just about all of the functionality. The test consists of the following steps:<p>
<ul>
<li>Create the columns that will be bound to the query. </li>
<li>Connect to the data source. </li>
<li>Create a query object from the database. </li>
<li>Bind the columns to the query. </li>
<li>Execute the query. </li>
<li>Fetch the results of the query.</li>
</ul>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include "SimpleDB/SimpleDB.h"</span>
<span class="keywordtype">int</span> main() {
<a name="_a0"></a><a class="code" href="classSimpleDB_1_1LongColumn.html">SimpleDB::LongColumn</a> <span class="keywordtype">id</span> = <a class="code" href="classSimpleDB_1_1LongColumn.html">SimpleDB::LongColumn</a>();
<a name="_a1"></a><a class="code" href="classSimpleDB_1_1StringColumn.html">SimpleDB::StringColumn</a> name = <a class="code" href="classSimpleDB_1_1StringColumn.html">SimpleDB::StringColumn</a>(1000);
<a name="_a2"></a><a class="code" href="classSimpleDB_1_1Column.html">SimpleDB::Column</a> * columns[2] = {&id,&name};
<span class="keywordflow">try</span> {
<a name="_a3"></a><a class="code" href="classSimpleDB_1_1Database.html">SimpleDB::Database</a> db = <a class="code" href="classSimpleDB_1_1Database.html">SimpleDB::Database</a>(<span class="stringliteral">"eminence"</span>);
<a name="_a4"></a><a class="code" href="classSimpleDB_1_1Query.html">SimpleDB::Query</a> query = db.<a name="a5"></a><a class="code" href="classSimpleDB_1_1Database.html#a1">newQuery</a>();
query.<a name="a6"></a><a class="code" href="classSimpleDB_1_1Query.html#a2">bind</a>(columns, <span class="keyword">sizeof</span>(columns)/<span class="keyword">sizeof</span>(columns[0]));
query.<a name="a7"></a><a class="code" href="classSimpleDB_1_1Query.html#a3">execute</a>(<span class="stringliteral">"SELECT contact_number, first_name FROM contacts LIMIT 10"</span>);
<span class="keywordflow">while</span>(query.<a name="a8"></a><a class="code" href="classSimpleDB_1_1Query.html#a4">fetchRow</a>())
std::cout << <span class="stringliteral">"ID: "</span> << *columns[0] << <span class="stringliteral">" NAME: "</span> << *columns[1] << std::endl;
query.<a class="code" href="classSimpleDB_1_1Query.html#a3">execute</a>(<span class="stringliteral">"SELECT contact_number, last_name FROM contacts LIMIT 5"</span>);
<span class="keywordflow">while</span>(query.<a class="code" href="classSimpleDB_1_1Query.html#a4">fetchRow</a>()) {
std::cout << <span class="stringliteral">"ID: "</span> << *columns[0] << <span class="stringliteral">" NAME: "</span> << *columns[1] << std::endl;
std::cout << <span class="stringliteral">"Some math "</span> << <span class="keywordtype">id</span>.value()*5 << <span class="stringliteral">" "</span> << name.value().append(<span class="stringliteral">" adfad"</span>) <<
std::endl;
}
} <span class="keywordflow">catch</span>(<a name="_a9"></a><a class="code" href="classSimpleDB_1_1Database_1_1Exception.html">SimpleDB::Database::Exception</a> &s) {
std::cerr << s << std::endl;
} <span class="keywordflow">catch</span>(<a name="_a10"></a><a class="code" href="classSimpleDB_1_1Column_1_1UnboundException.html">SimpleDB::Column::UnboundException</a>) {
std::cerr << <span class="stringliteral">"A column was not bound!"</span> << std::endl;
}
<span class="keywordflow">return</span> 0;
}
</pre></div> <hr>
<div align="right"><small>
Copyright (C) 2005 <a target="top" href="http://www.eminence.com.au">Eminence Technology Pty Ltd</a>.<br>
Documentation pages generated by doxygen.</small><br>
<a href="http://sourceforge.net"><img src="http://sourceforge.net/sflogo.php?group_id=129109&type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" /></a>
</div>
|