⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 overview.htm

📁 一个通用的oracle OCI开发程序包
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>Overview</title>
</head>

<body bgcolor="#FFFFC0">

<h1>Overview</h1>

<ul type="disc">
    <li><a href="#Relational">Relational</a></li>
    <li><a href="#Object">Object</a></li>
</ul>

<p>There are two ways to get data into and out of an Oracle 8.x
database with OCI++.</p>

<ul type="disc">
    <li>Relational.</li>
    <li>Object.</li>
</ul>

<p>Which ever method you choose, a connection to the server must
be established. This is achieved through the COCISession object.
e.g. </p>

<pre><font color="#FF0000">COCISession</font> s1; 
s1.connect(&quot;username/password@db&quot;)</pre>

<h3><a name="Relational"></a>Relational</h3>

<p>As with all data, there will be four things that you will want
to do to it, create, read, update, and delete it. The relational
method deals with these operations by way of a SQL statement.
This SQL statement is captured in OCI++ with the aid of the
COCIStatement object, e.g. COCIStatement stmt(s1); stmt =
&quot;select id from emp&quot;; . In order to retrieve data, i.e.
a read operation, we must define a place holder to 'capture' the
data being returned from the database. So, in the above example,
the container for the captured data will be an 'int'. Once all
placeholders have been set up, we must ask the database for the
data. This is achieved through executing the statement. If all
rows are not retrieved, then further requests are made to the
database by way of the fetch method. So, the following code will
bring us back some data:-</p>

<h4>Data retrieval using the relational method..</h4>

<pre><font color="#FF0000">COCIStatement</font> stmt(s1);	        <font
color="#008000">// Note s1 is the session that we've already established with the database</font>
stmt = &quot;select id from emp&quot;; 	<font color="#008000">// Assume a table exists on the database called emp, containing an attribute id</font>
int id = 0;			<font color="#008000">// The container to capture the returned data (in this case initialised to zero)</font>
stmt.define(1,id); 		<font color="#008000">// Use the method define on the COCIStatement class to retrieve data.</font> 
				<font color="#008000">// The first parameter is 1, as it is the first position place holder in the select list.</font>
if(stmt.execute())		<font color="#008000">// Can we execute the statement, if so, then id will now contain the first row of data</font>
{
  do
  {
    // Do something on id
  }while(stmt.fetch());		<font color="#008000">// Do, while there are more rows to fetch...</font>
}</pre>

<p>&nbsp;</p>

<h4>Data input using the relational method.</h4>

<pre><font color="#FF0000">COCIStatement</font> stmt(s1);			<font
color="#008000">// Note s1 is the session that we've already established with the database</font>
stmt = &quot;insert into emp values (:id)&quot;;  <font
color="#008000">// Set up to insert a new emp row containing a value id</font>
int id = 42;				<font color="#008000">// Now the container acts as an input data-source</font>
stmt.bind(&quot;:id&quot;,id); 			<font color="#008000">// Use the method bind on the COCIStatement class to insert data.</font> 
if(stmt.execute())			<font color="#008000">// Can we execute the statement, if so, then a new row will exist on the database,</font>
{					<font color="#008000">// containing a value of 42 in id</font>
}</pre>

<p>&nbsp;</p>

<p>That's effectively it for relational data retrieval (update is
another form of input (hence bind), and delete will only use bind
when the amount of data to delete is restricted). The above data
retrieval methods will act as the building blocks for getting
data into and out of a database. The only things that will vary,
will be array fetching (i.e. providing more containers for less
network round-trips), and the datatypes. So, a couple of key
points to remember are:-</p>

<ul type="disc">
    <li>Bind - used for data input (write)</li>
    <li>Define - used for data output (read)</li>
</ul>

<h3><a name="Object"></a>Object</h3>

<p>This is the new extension to Oracle for versions 8.x.
Effectively, objects are created in the server, based upon a
type. e.g.</p>

<pre><font color="#0000FF">create type emp_t as object (id number);
create table emp of emp_t;</font></pre>

<p>As in the above example, we can create <font color="#0000FF">emp</font>
objects, based upon the type <font color="#0000FF">emp_t</font>.
This OCI++ system captures this style of object creation. Simply
put, we can create persistent and non-persistent objects.
Persistent (as the name suggests), allows the object created
within our C++ program to live longer than the program itself
(i.e. it will become stored in the database). Non-persistent
object (i.e. transient), will only live as long as a transaction
(the transaction is the unit of database work). So, to create an
object that will persist on the database, we use the COCIObject
class, i.e.</p>

<h4>Persistent object</h4>

<pre><font color="#FF0000">COCIObject</font> <font
color="#008080">emp</font>(s1, <font color="#FF0000">COCITable</font>(s1, &quot;EMP&quot;), <font
color="#FF0000">COCIType</font>(s1, &quot;EMP_T&quot;));</pre>

<p>Here, the object <font color="#008080">emp</font>, is based
upon the Oracle table <font color="#0000FF">emp</font>, and the
Oracle type <font color="#0000FF">emp_t</font>. A non-persistent
emp object is created without a table object (COCITable), i.e.</p>

<h4>Non-persistent object</h4>

<pre><font color="#FF0000" face="Times New Roman">C</font><font
color="#FF0000">OCIObject</font> <font color="#008080">emp</font>(s1, <font
color="#FF0000">COCIType</font>(s1, &quot;EMP_T&quot;));</pre>

<p>Data is set and retrieved from these objects by way of get/set
accessor methods.</p>

<pre>int id = <font color="#008080">emp</font>.get(&quot;id&quot;);</pre>

<p>The above statement will get the id attribute from the emp
object. Similarly for setting, there is a set accessor</p>

<pre>int id = 42;
<font color="#008080">emp</font>.set(&quot;id&quot;,id);</pre>

<p>Commiting a transaction will put the data back on to the
server. So the above persistent object will be made persistent by
the following code sample:-</p>

<pre><font color="#FF0000">COCITransaction</font> t(s1);
t.start();	// For a single transaction, explicit starts can be left out

<font color="#FF0000">COCIObject</font> emp(s1, <font
color="#FF0000">COCITable</font>(s1, &quot;EMP&quot;), <font
color="#FF0000">COCIType</font>(s1, &quot;EMP_T&quot;));
int id = 42;
emp.set(&quot;id&quot;,id);

t.commit();</pre>

<p>Again, as in the case of Relational, data is retrieved into
different datatypes (e.g. COCIRef, COCILob, COCIString, etc.).
You will find that the datatypes themeselves are a 1:1 mapping of
the Oracle datatypes.</p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -