如何使用SOCI从数据库中获取整行? [英] How to get a whole row from database using SOCI?

查看:128
本文介绍了如何使用SOCI从数据库中获取整行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

... and save it into self-defined object type? I'm using PostgreSQL. When I have everything in one file, it works. But I wanted to split this into class-files like you always do when writing in cpp. When I divided my code into *.h and *.cpp files, I'm getting errors.

这是我的文件:

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

main.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

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回答。

推荐答案

专门用于type_conversion的代码

The code where you specialize type_conversion

template<>
struct type_conversion<MyInt>

需要在test.h中而不是test.cpp中。问题是,如果像现在一样将其保存在test.cpp中,则在使用SOCI的main.cpp中不可见

Needs to be in test.h not test.cpp. The problem is if you have it in test.cpp like you do now, it is not visible in main.cpp where you are using SOCI

这篇关于如何使用SOCI从数据库中获取整行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆