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

问题描述

我扩展了我的类(从这个问题:)有两个私有成员,还添加了getters和setter。但是在编译程序时,我再次出现错误。

I extended my class (from this question: How to get a whole row from database using SOCI?) to have two private members, also added getters and setters. But when compiling my program, I have errors again.

myClass.h

#include <soci.h>
#include <postgresql/soci-postgresql.h>
#include <string>

class MyClass
{
public:
    MyClass();
    MyClass(int i, std::string);

    void setI(int i);
    int getI() const;
    void setS(std::string s);
    std::string getS() const;

private:
    int i_;
    std::string s_;
};

namespace soci
{
    template <>
    struct type_conversion<MyClass>
    {
        typedef MyClass base_type;

        static void from_base(int i, std::string s, soci::indicator ind, MyClass & mi)
        {
            if (ind ==  soci::i_null)
            {
                throw soci_error("Null value not allowed for this type");
            }

            mi.setI(i);
        mi.setS(s);
        }

        static void to_base(const MyClass & mi, int & i,  std::string &s, soci::indicator & ind)
        {
            i = mi.getI();
        s = mi.getS();
            ind = soci::i_ok;
        }
    };
}

myClass.cpp
$ b

myClass.cpp

#include "myClass.h"

MyClass::MyClass()
{

}

MyClass::MyClass(int i, std:string s)
{
    this->i_ = i;
    this->s_ = s;
}

int MyClass::getI() const
{
    return this->i_;
}

void MyClass::setI(int i)
{
    this->i_ = i;
}

std::string MyClass::getS() const
{
    return this->s_;
}

void MyClass::setS(std::string s)
{
    this->s_ = s;
}

myClassTest.cpp
$ b

myClassTest.cpp

#include <iostream>
#include "myClass.h"

int main(int argc, char **argv)
{

    soci::session sql;
    sql.open(soci::postgresql, "dbname=mydb user=postgres password=postgrespass");
    MyClass i;
    sql << "SELECT id, name FROM person;", soci::into(i);
    std::cout << i.getI() << " " << i.getS();
    sql.close();

    return 0;
}

我这样编译:

我得到的错误是:

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 myClass.h:1,
                 from myClassTest.cpp:2:
/usr/local/include/soci/exchange-traits.h: In instantiation of ‘soci::details::exchange_traits<MyClass>’:
/usr/local/include/soci/into.h:29:60:   instantiated from ‘soci::details::into_type_ptr soci::into(T&) [with T = MyClass, soci::details::into_type_ptr = soci::details::type_ptr<soci::details::into_type_base>]’
myClassTest.cpp:27:65:   instantiated from here
/usr/local/include/soci/exchange-traits.h:35:5: error: incomplete type ‘soci::details::exchange_traits<MyClass>’ used in nested name specifier
In file included from /usr/local/include/soci/into.h:13:0,
                 from /usr/local/include/soci/soci.h:22,
                 from myClass.h:1,
                 from myClassTest.cpp:2:
/usr/local/include/soci/type-conversion.h: In member function ‘void soci::details::conversion_into_type<T>::convert_from_base() [with T = MyClass]’:
myClassTest.cpp:34:1:   instantiated from here
/usr/local/include/soci/type-conversion.h:59:9: error: no matching function for call to ‘soci::type_conversion<MyClass>::from_base(soci::type_conversion<MyClass>::base_type&, soci::indicator&, MyClass&)’
/usr/local/include/soci/type-conversion.h:59:9: note: candidate is:
myClass.h:28:21: note: static void soci::type_conversion<MyClass>::from_base(int, std::string, soci::indicator, MyClass&)
myClass.h:28:21: note:   candidate expects 4 arguments, 3 provided
myClass.cpp:8:28: error: found ‘:’ in nested-name-specifier, expected ‘::’


推荐答案

你尝试SOCI 3.2吗?我做了类似的这个版本的工作很好:

Have you tried SOCI 3.2? I did something similar with this version which worked pretty well:

vector<Entry> Entry::findByName(string name) {

    Entry entry;

    vector<Entry> entries;

    //Starting a connection to database
    session sql(firebird, "service=/srv/firebird/registry.gdb user=SYSDBA password=password");

    //Querying data using a prepared statement and place data into a Entry object
    statement st = (sql.prepare <<
                "select NAME, ADDRESS, PHONE from ENTRY WHERE NAME like '%' || :NAME || '%'",
                into(entry), use(name));
    st.execute();

    //Checking if we can fetch a row from resultset
    while (st.fetch())
    {
        //Pushing the object with mapped data into the entries vector
        entries.push_back(entry);
    }

    return entries;
}

您可以将其更改为:

Entry Entry::findByName(string name) {

    Entry entry;

    //Starting a connection to database
    session sql(firebird, "service=/srv/firebird/registry.gdb user=SYSDBA password=password");

    //Querying data using a prepared statement and place data into a Entry object
    statement st = (sql.prepare <<
                "select NAME, ADDRESS, PHONE from ENTRY WHERE NAME like '%' || :NAME || '%'",
                into(entry), use(name));
    st.execute();

    //Checking if we can fetch a row from resultset
    if (st.fetch())
    {
        return entry;
    }

    return NULL;
}



注意:这两个都是我的模型类的静态类, ActiveRecord的版本。

Note: Both of them are static classes of my model class which implements a custom version of ActiveRecord.

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

06-26 04:18