📄 otl4_ex391.htm
字号:
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="Author" content="Sergei Kuchin">
<meta name="GENERATOR"
content="Mozilla/4.75 [en] (Win98; U) [Netscape]">
<meta name="KeyWords"
content="OTL, Oracle, ODBC, DB2, CLI, database API, C++, Template Library">
<title>OTL 4.0, Example 391 (Insert/Update/Select with MySQL LONGTEXT
in stream mode, without otl_lob_stream::set_Len())</title>
</head>
<body>
<center>
<h1>OTL 4.0, Example 391 (Insert/Update/Select with MySQL LONGTEXT in
stream
mode, without otl_lob_stream::set_len())</h1>
</center>
This example demonstrates INSERT, UPDATE, and SELECT statements with
the
MySQL LONGTEXT datatype in the <a href="otl3_lob_stream.htm">stream
mode</a>, without otl_lob_stream::set_len() in OTL 4.0.138 and higher.<br>
<h2>Source Code</h2>
<pre>#include <iostream><br>using namespace std;<br><br>#include <stdio.h></pre>
<pre>#define <a href="otl3_compile.htm#OTL_ODBC">OTL_ODBC</a> // Compile OTL 4.0/ODBC<br>// The following #define is required with MyODBC 3.51.11 and higher<br>#define <a
href="otl3_compile.htm#OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE">OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE</a><br>#include <otlv4.h> // include the OTL 4.0 header file<br><br><a
href="otl3_connect_class.htm">otl_connect</a> db; // connect object<br><br>void insert()<br>// insert rows into table<br>{<a
href="otl3_long_string.htm">otl_long_string</a> f2(6000); // define long string variable<br> <a
href="otl3_stream_class.htm">otl_stream</a> o; // defined an otl_stream variable<br> o.<a
href="otl3_stream_class.htm#set_lob_stream_mode">set_lob_stream_mode</a>(true); // set the "lob stream mode" flag<br> o.<a
href="otl3_stream_class.htm#open">open</a>(1, // buffer size has to be set to 1 for operations with LOBs<br> "insert into test_tab values(:f1<int>,:f2<varchar_long>, "<br> ":f3<varchar_long>) ",<br> // SQL statement<br> db // connect object<br> );<br> o.<a
href="otl3_stream_class.htm#set_commit">set_commit</a>(0); // setting stream "auto-commit" to "off". It is required<br> // when LOB stream mode is used.</pre>
<pre> int i,j;<br> <a href="otl3_lob_stream.htm">otl_lob_stream</a> lob; // LOB stream for reading/writing unlimited number<br> // of bytes regardless of the buffer size.<br> <a
href="otl3_lob_stream.htm">otl_lob_stream</a> lob2; // LOB stream for reading/writing unlimited number<br> // of bytes regardless of the buffer size.<br><br> for(i=1;i<=20;++i){<br> o<<i;<br> o<a
href="otl3_stream_class.htm#stream_write_lob"><<</a>lob; // Initialize otl_lob_stream by writing it<br> // into otl_stream.<br> o<a
href="otl3_stream_class.htm#stream_write_lob"><<</a>lob2; // Initialize otl_lob_stream by writing it<br> // into otl_stream.<br><br> for(j=0;j<5000;++j)<br> f2[j]='*';<br> f2[5000]='?';<br> f2.<a
href="otl3_long_string.htm#set_len">set_len</a>(5001);<br><br>// OTL 4.0.138 and higher does not require the LOB's total length to be <br>// set beforehand. Instead, otl_long_string::last_piece can be used.<br>// lob.<a
href="otl3_lob_stream.htm#set_len">set_len</a>(5001+2123); // setting the total size of<br>// // the LONGTEXT to be written.<br> <br> lob<a
href="otl3_lob_stream.htm#write"><<</a>f2; // writing first chunk of the LONGTEXT into lob<br><br><br> f2[2122]='?';<br> f2.set_len(2123); // setting the size of the second chunk<br><br> lob<<f2; // writing the second chunk of the LONGTEXT into lob<br> lob.<a
href="otl3_lob_stream.htm">close</a>(); // closing the otl_lob_stream<br><br> for(j=0;j<5000;++j)<br> f2[j]='*';<br> f2[5000]='?';<br> f2.<a
href="otl3_long_string.htm#set_len">set_len</a>(5001);<br>// OTL 4.0.138 and higher does not require the LOB's total length to be <br>// set beforehand. Instead, otl_long_string::last_piece can be used.<br>// lob2.<a
href="otl3_lob_stream.htm#set_len">set_len</a>(5001+2123); // setting the total size of<br>// // the LONGTEXT to be written.<br> <br> lob2<a
href="otl3_lob_stream.htm#write"><<</a>f2; // writing first chunk of the LONGTEXT into lob<br><br> f2[2122]='?';<br> f2.set_len(2123); // setting the size of the second chunk<br><br> lob2<<f2; // writing the second chunk of the LONGTEXT into lob<br> lob2.<a
href="otl3_lob_stream.htm">close</a>(); // closing the otl_lob_stream<br><br> }<br><br> db.commit(); // committing transaction.<br>}</pre>
<pre>void update()<br>// insert rows in table<br>{</pre>
<pre> <a href="otl3_long_string.htm">otl_long_string</a> f2(6200); // define long string variable<br><br> <a
href="otl3_stream_class.htm">otl_stream</a> o; // defined an otl_stream variable<br> o.<a
href="otl3_stream_class.htm#set_lob_stream_mode">set_lob_stream_mode</a>(true); // set the "lob stream mode" flag<br> o.<a
href="otl3_stream_class.htm#open">open</a>(1, // buffer size has to be set to 1 for operations with LOBs<br> "update test_tab "<br> " set f2=:f2<varchar_long> "<br> "where f1=:f1<int> ",<br> // SQL statement<br> db // connect object<br> );<br><br> <a
href="otl3_lob_stream.htm">otl_lob_stream</a> lob;<br><br> o.<a
href="otl3_stream_class.htm#set_commit">set_commit</a>(0); // setting stream "auto-commit" to "off". <br><br><br> for(int j=0;j<6000;++j){<br> f2[j]='#';<br> }<br><br> f2[6000]='?';<br> f2.<a
href="otl3_long_string.htm#set_len">set_len</a>(6001);<br><br> o<a
href="otl3_stream_class.htm#stream_write_lob"><<</a>lob; // Initialize otl_lob_stream by writing it<br> // into otl_stream.<br> o<<5;<br><br>// lob.<a
href="otl3_lob_stream.htm#set_len">set_len</a>(6001*4); // setting the total size of of the LONGTEXT to be written<br> for(int i=1;i<=4;++i)<br> lob<a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -