📄 dbviewwritedata.htm
字号:
<pre><code><span class="codeComment">// Using a DBView to insert rows into a database</span>
<span class="codeComment">// ... Class definitions for Example and BCAExample as per our </span><a
href="DBViewReadData.htm">ReadData</a> <span class="codeComment">example .....
// Specialization of DefaultInsValidate for Example
// This defines a business rule we wish to enforce for all
// Example objects before they are allowed to be inserted into the database</span>
template<> class dtl::DefaultInsValidate<Example>
{
public:
bool operator()(Example &rowbuf) {
<span class="codeComment">// data is valid if rowbuf.exampleStr is nonempty and
// rowbuf.exampleDouble is
// between 0 and 100 (like a percentage)</span>
return (rowbuf.exampleStr.length() > 0 && rowbuf.exampleDouble >= 0.0
&& rowbuf.exampleLong <= 100.0);
}
};
<span class="codeComment">// Insert rows from the vector<Example> parameter into the database</span>
void WriteData(const vector<Example> &examples)
{
DBView<Example> view("DB_EXAMPLE");
<span class="codeComment">// loop through vector and write Example objects to DB</span>
// write_it.GetCount() records written in loop
DBView<Example>::insert_iterator write_it = view;
for (vector<Example>::const_iterator ex_it = examples.begin(); ex_it != examples.end(); ex_it++, write_it++)
{
*write_it = *ex_it;
cout << "Writing element #" << write_it.GetCount() + 1<< endl;
}
}
</code></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -