C++ 如何使用SOCI从数据库中获取整行数据?
。。。并将其保存到自定义对象类型中?我正在使用PostgreSQL。当我把所有东西都放在一个文件中时,它就工作了。但是我想把它分成类文件,就像你在cpp中写东西时经常做的那样。当我将代码划分为*.h和*.cpp文件时,会出现错误 这是我的档案: 测试.hC++ 如何使用SOCI从数据库中获取整行数据?,c++,postgresql,postgresql-9.1,soci,C++,Postgresql,Postgresql 9.1,Soci,。。。并将其保存到自定义对象类型中?我正在使用PostgreSQL。当我把所有东西都放在一个文件中时,它就工作了。但是我想把它分成类文件,就像你在cpp中写东西时经常做的那样。当我将代码划分为*.h和*.cpp文件时,会出现错误 这是我的档案: 测试.h class MyInt { public: MyInt(); MyInt(int i); void set(int i); int get() const; private: int i_; };
class MyInt
{
public:
MyInt();
MyInt(int i);
void set(int i);
int get() const;
private:
int i_;
};
测试.cpp
#include "test.h"
#include <soci.h>
#include <postgresql/soci-postgresql.h>
MyInt::MyInt()
{
}
MyInt::MyInt(int i)
{
this->i_ = i;
}
int MyInt::get() const
{
return this->i_;
}
void MyInt::set(int i)
{
this->i_ - i;
}
namespace soci
{
template <>
struct type_conversion<MyInt>
{
typedef int base_type;
static void from_base(int i, soci::indicator ind, MyInt & mi)
{
if (ind == soci::i_null)
{
throw soci_error("Null value not allowed for this type");
}
mi.set(i);
}
static void to_base(const MyInt & mi, int & i, soci::indicator & ind)
{
i = mi.get();
ind = soci::i_ok;
}
};
}
#include <iostream>
#include "test.h"
int main(int argc, char **argv)
{
MyInt i;
sql.open(soci::postgresql, "dbname=mydb user=postgres password=postgrespass");
sql << "SELECT count(*) FROM person;", soci::into(i);
std::cout << "We have " << i.get() << " persons in the database.\n";
sql.close();
return 0;
}
#包括“test.h”
#包括
#包括
MyInt::MyInt()
{
}
MyInt::MyInt(inti)
{
这->我=我;
}
int MyInt::get()常量
{
返回此->i;
}
void MyInt::set(int i)
{
这个->i_ui;
}
名称空间社会
{
模板
结构类型转换
{
typedef int base_type;
来自_基的静态无效(int i、soci::indicator ind、MyInt和mi)
{
if(ind==soci::i_null)
{
抛出soci_错误(“此类型不允许空值”);
}
mi.set(i);
}
静态空至_基准(常量MyInt&mi、int&i、soci::indicator&ind)
{
i=mi.get();
ind=社会::i_ok;
}
};
}
main.cpp
#include "test.h"
#include <soci.h>
#include <postgresql/soci-postgresql.h>
MyInt::MyInt()
{
}
MyInt::MyInt(int i)
{
this->i_ = i;
}
int MyInt::get() const
{
return this->i_;
}
void MyInt::set(int i)
{
this->i_ - i;
}
namespace soci
{
template <>
struct type_conversion<MyInt>
{
typedef int base_type;
static void from_base(int i, soci::indicator ind, MyInt & mi)
{
if (ind == soci::i_null)
{
throw soci_error("Null value not allowed for this type");
}
mi.set(i);
}
static void to_base(const MyInt & mi, int & i, soci::indicator & ind)
{
i = mi.get();
ind = soci::i_ok;
}
};
}
#include <iostream>
#include "test.h"
int main(int argc, char **argv)
{
MyInt i;
sql.open(soci::postgresql, "dbname=mydb user=postgres password=postgrespass");
sql << "SELECT count(*) FROM person;", soci::into(i);
std::cout << "We have " << i.get() << " persons in the database.\n";
sql.close();
return 0;
}
#包括
#包括“test.h”
int main(int argc,字符**argv)
{
MyInt i;
open(soci::postgresql,“dbname=mydb user=postgres password=postgrespass”);
sql专门化类型转换的代码
template<>
struct type_conversion<MyInt>
模板
结构类型转换
需要在test.h而不是test.cpp中。问题是如果你像现在一样在test.cpp中有它,那么它在使用SOCI的main.cpp中是不可见的。@Brian:这是一个完全不同的问题,需要单独发布。@JohnBandela:谢谢!它起作用了!:)但是现在我有一个类似的问题-我扩展了我的类以喜欢两个私人我mbes-我现在如何从数据库中获取一行并将其保存到我的自定义对象中?您能帮我解决这个问题吗?请参阅此主题:如果可以,谢谢:)