【问题标题】:How to get a whole row from database using SOCI?如何使用 SOCI 从数据库中获取整行?
【发布时间】:2012-11-29 19:51:25
【问题描述】:

...并将其保存为自定义对象类型?我正在使用 PostgreSQL。当我将所有内容都放在一个文件中时,它就可以工作。但我想把它分成类文件,就像你在用 cpp 编写时所做的那样。当我将代码分成 *.h 和 *.cpp 文件时,我遇到了错误。

这是我的文件:

test.h

class MyInt
{
public:
    MyInt();
    MyInt(int i);

    void set(int i);
    int get() const;

private:
    int i_;
};

test.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;
        }
    };
}

ma​​in.cpp

#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;
}

我是这样编译的:

g++ main_test.cpp test.h test.cpp -o App -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci -I /usr/include/postgresql

并得到了这些错误:

In file included from /usr/local/include/soci/into-type.h:13:0,
                 from /usr/local/include/soci/blob-exchange.h:12,
                 from /usr/local/include/soci/soci.h:18,
                 from main_test.cpp:3:
/usr/local/include/soci/exchange-traits.h: In instantiation of â€soci::details::exchange_traits<MyInt>’:
/usr/local/include/soci/into.h:29:60:   instantiated from â€soci::details::into_type_ptr soci::into(T&) [with T = MyInt, soci::details::into_type_ptr = soci::details::type_ptr<soci::details::into_type_base>]’
main_test.cpp:29:59:   instantiated from here
/usr/local/include/soci/exchange-traits.h:35:5: error: incomplete type â€soci::details::exchange_traits<MyInt>’ used in nested name specifier

上述问题已解决,请查看@JohnBandela ANSWER。

【问题讨论】:

    标签: c++ postgresql postgresql-9.1 soci


    【解决方案1】:

    你擅长 type_conversion 的代码

    template<>
    struct type_conversion<MyInt>
    

    需要在 test.h 而不是 test.cpp 中。问题是如果你像现在一样在 test.cpp 中拥有它,它在你使用 SOCI 的 main.cpp 中不可见

    【讨论】:

    • @Brian :这是一个完全不同的问题,需要单独发帖。
    • @JohnBandela:谢谢!有效! :) 但是现在我有一个类似的问题 - 我扩展了我的课程以支持两个私有成员 - 我现在如何从数据库中获取一行并将其保存到我的自定义对象中?你会这么好心并帮助我解决这个问题吗?请看这个话题:stackoverflow.com/questions/13635616/…如果可以的话,谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2012-07-21
    • 2012-04-27
    • 1970-01-01
    • 2021-09-16
    • 2021-08-30
    • 2011-11-06
    • 2020-06-14
    • 2017-07-10
    相关资源
    最近更新 更多