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

📄 dbviewupdatedata.htm

📁 The goal of this library is to make ODBC recordsets look just like an STL container. As a user, you
💻 HTM
字号:
<pre><code><span class="codeComment">//Update objects in the database via an update_iterator</span>


class Example
{
  public:                                <span class="codeComment">// tablename.columnname:</span>
	int exampleInt;                 <span class="codeComment">// DB_EXAMPLE.INT_VALUE</span>
	string exampleStr;              <span class="codeComment">// DB_EXAMPLE.STRING_VALUE</span>
	double exampleDouble;           <span class="codeComment">// DB_EXAMPLE.DOUBLE_VALUE</span>
	long exampleLong;               <span class="codeComment">// DB_EXAMPLE.EXAMPLE_LONG</span>
	TIMESTAMP_STRUCT exampleDate;   <span class="codeComment">// DB_EXAMPLE.EXAMPLE_DATE</span>

	Example(int exInt, const string &amp;exStr, double exDouble, long exLong,
		const TIMESTAMP_STRUCT &amp;exDate) :
	   exampleInt(exInt), exampleStr(exStr), exampleDouble(exDouble), exampleLong(exLong),
	   exampleDate(exDate)
	{ }

};

class BCAExampleObj
{
public:
	void operator()(BoundIOs &amp;boundIOs, Example &amp;rowbuf)
	{
		boundIOs[&quot;INT_VALUE&quot;] == rowbuf.exampleInt;
		boundIOs[&quot;STRING_VALUE&quot;] == rowbuf.exampleStr;
		boundIOs[&quot;DOUBLE_VALUE&quot;] == rowbuf.exampleDouble;
		boundIOs[&quot;EXAMPLE_LONG&quot;] == rowbuf.exampleLong;
		boundIOs[&quot;EXAMPLE_DATE&quot;] == rowbuf.exampleDate;
	}
};

class ExampleParamObj
{
    public:
       	int lowIntValue;
	int highIntValue;
	string strValue;
	TIMESTAMP_STRUCT dateValue;
};

class BPAParamObj
{
public:
	void operator()(BoundIOs &amp;boundIOs, ExampleParamObj &amp;paramObj)
	{
	  boundIOs[0] == paramObj.lowIntValue;
	  boundIOs[1] == paramObj.highIntValue;
	  boundIOs[2] == paramObj.strValue;
	  boundIOs[3] == paramObj.dateValue;
	}

};


<span class="codeComment">// update Example object (with new values) meeting a query in the database</span>
void UpdateData()
{ 
	<span class="codeComment">// construct view</span>
	DBView&lt;Example, ExampleParamObj&gt;
	view(&quot;DB_EXAMPLE&quot;, BCAExampleObj(), 
	&quot;WHERE INT_VALUE BETWEEN (?) AND (?) AND &quot;
	&quot;STRING_VALUE = (?) OR EXAMPLE_DATE = (?)&quot;, BPAParamObj());

	<span class="codeComment">// build an updater for the view

	// *** SQL Query Generated for this update_iterator:&quot; ***
	// *** (Note: All column and field names arealphabetized due to our implementation) ***
	// &quot;UPDATE DB_EXAMPLE SET DOUBLE_VALUE = (?), EXAMPLE_DATE = (?), EXAMPLE_LONG = (?), INT_VALUE = (?), &quot;
	// &quot;STRING_VALUE = (?) WHERE INT_VALUE BETWEEN (?) AND (?) AND STRING_VALUE = (?) OR EXAMPLE_DATE = (?)&quot;
	</span>
	DBView&lt;Example, ExampleParamObj&gt;::update_iterator

	exampleUpdater = view;

	<span class="codeComment">// set data fields we want to update to their desired values
	// exampleStr to &quot;Updated&quot; andsampleLong to 0</span>
	Example updateMe;

	updateMe.exampleStr = &quot;Updated&quot;;
	updateMe.exampleLong = 25;

	TIMESTAMP_STRUCT today = {2000, 9, 29, 0, 0, 0,0};

	updateMe = Example(2121, &quot;Updated&quot;, 99.99, 25, today);

	*exampleUpdater = updateMe;

	<span class="codeComment">// now set the parameters indicating which rows</span>
	we want the update applied
	exampleUpdater.Params().lowIntValue = 5;
	exampleUpdater.Params().highIntValue = 13;
	exampleUpdater.Params().strValue = &quot;FindMe&quot;;

	TIMESTAMP_STRUCT paramDate = {1999, 11, 11, 0,0, 0, 0};
	exampleUpdater.Params().dateValue = paramDate;

	<span class="codeComment">// execute the update</span>
	exampleUpdater++;

	cout &lt;&lt; exampleUpdater.GetLastCount()
	&lt;&lt; &quot; rows updated!&quot; &lt;&lt; endl;

	<span class="codeComment">// now can perform other updates using the same updater object
	// make sure to put in your new values for both the data and parameter values
	// for the update
	// set data fields we want to update to their desired values
	// exampleStr to &quot;Second Update&quot; and exampleLong to 66</span>
	TIMESTAMP_STRUCT tomorrow = {2000, 9, 30, 0, 0,0, 0};
	updateMe = Example(2222, &quot;Second Update&quot;, 0.11111, 66, tomorrow);
	updateMe.exampleLong = 66;

	*exampleUpdater = updateMe;

	<span class="codeComment">// now set the parameters indicating which rows</span>
	we want the update applied
	exampleUpdater.Params().lowIntValue = 21;
	exampleUpdater.Params().highIntValue = 30;
	exampleUpdater.Params().strValue = &quot;To
	find&quot;;

	TIMESTAMP_STRUCT otherParamDate = {2001, 10, 31,0, 0, 0, 0};
	exampleUpdater.Params().dateValue = otherParamDate;

	<span class="codeComment">// execute the update</span>
	exampleUpdater++;

	cout &lt;&lt; exampleUpdater.GetLastCount() &lt;&lt; &quot; rows updated!&quot; &lt;&lt; endl;
}
</code></pre>

⌨️ 快捷键说明

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