因此,我正在对某人的类进行代码检查,将程序的输出转储到数据库中。因此,他们得到了一组成员的struct Foo列表。现在,它们在类中有一个成员变量,并且正在复制每个调用上的值,而不是更改SQLBindParameter表。
struct Foo
{
int bar;
int baz;
};
class SQLWriter
{
public:
SQLWriter()
{
//initializes SQLHSTMT hQuery to something that takes two ? inputs
SQLBindParameter(hQuery,0,SQL_PARAM_INPUT,SQL_C_INT, 0, 0, &bar_, 0, NULL);
SQLBindParameter(hQuery,1,SQL_PARAM_INPUT,SQL_C_INT, 0, 0, &baz_, 0, NULL);
}
void WriteFoos(const std::vector<Foo> foos)
{
for (int i = 0; i < foos.size(); i++)
{
const Foo& foo = foos[i];
bar_ = foo.bar;
baz_ = foo.baz;
SQLExecute(hQuery);
}
}
private:
SQLHSTMT hQuery; int bar_; int baz_;
};这个seems...insane对我来说,但我真的不知道数据库的东西,我更多的只是一个C++程序。在我看来,这样做是正确的:
struct Foo
{
int bar;
int baz;
};
class SQLWriter
{
public:
SQLWriter()
{
//initializes SQLHSTMT hQuery to something that takes two ? inputs
}
void WriteFoos(const std::vector<Foo> foos)
{
for (int i = 0; i < foos.size(); i++)
{
const Foo& foo = foos[i];
SQLBindParameter(hQuery,0,SQL_PARAM_INPUT,SQL_C_INT, 0, 0, &foo.bar, 0, NULL);
SQLBindParameter(hQuery,1,SQL_PARAM_INPUT,SQL_C_INT, 0, 0, &foo.baz, 0, NULL);
SQLExecute(hQuery);
}
}
private:
SQLHSTMT hQuery;
};这样,写调用就不会有那些奇怪的副作用和所有多余的变量。因为foo真的有更多的变量(10岁),这似乎更明智。我说错了吗?
发布于 2014-08-21 14:57:19
SQLBindParameter()的优点或有用之处在于,您可以绑定到缓冲区,只需在每个SQLExecute()之前更改缓冲区。
虽然按照您在第一个代码片段中显示的方式进行重新绑定可能会更加糟糕,但每次重新绑定可能会花费很大的代价,这取决于它发生了多少次。
Mungflesh提出了一个很好的观点,我同意,与代码的整洁程度相比,性能可能会成为一个更大的问题。
您可能仍然能够找到一种清理代码的方法,但是请记住,如果没有必要的话,您最好避免重新绑定。
https://stackoverflow.com/questions/20642020
复制相似问题