- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我扩展了我的类(来自这个问题:How to get a whole row from database using SOCI?)以拥有两个私有(private)成员,还添加了 getter 和 setter。但是在编译我的程序时,我又出错了。
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
#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
#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;
}
我是这样编译的:
g++ myClassTest.cpp myClass.h myClass.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 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?我对这个版本做了类似的事情,效果很好:
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。
关于c++ - 如何使用 SOCI 从数据库中获取整行到用户定义的对象类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13635616/
我真的很困惑,我被这个问题困住了,希望你能帮助我: 我使用:Windows 8.1、Visual Studio Express 2013 和 SOCI 3.2.2 我已经编译了 libsoci_mys
我想从名为“person”的表中获取行。我想借助指标来做到这一点,以避免在该人没有名字时出现异常。如何做到这一点? 我写的代码: try { soci::statement st = (sql.pre
我正在尝试使用 SOCI 库来轻松访问 MySQL,但我无法安装该库。 到目前为止我所做的是: 创建项目并使用 CMake 编译 将我的项目链接到编译后创建的 lib/release 目录。 将我的项
我有这样一个函数: CREATE OR REPLACE FUNCTION get_path_set_1(IN pathset_id_in character varying, OUT id chara
我使用 Soci进行数据库查询。现在我需要一个自定义结果集类,它将环绕 soci::rowset。我无法按照下面的代码方式让它工作,仅仅是因为复制构造函数在 soci 中是私有(private)的(根
我正面临一个烦人的问题,这个问题让我有一段时间没有编程了。我打算开始一个个人项目,在这个项目中我需要使用数据库来存储某些信息,我决定使用 SQLite,但是我不喜欢 C-ish API,所以我在 SQ
我正在使用 SOCI 访问 PostgreSQL 数据库。我要插入并从中选择的一个特定表(目前)有 72 列。我的问题是如何最好地处理这么多列? 我已经确定,对于选择,使用 SOCI 动态结果集可能是
假设我有以下功能。如果出现无效参数或异常,该函数必须以空 rowset 退出。 rowset SelectAllFromTable(string tableName) { session sq
在 soci ( http://soci.sourceforge.net/ ) 中,可以一次执行多个 SQL 语句吗?例如: session 26; update my_option_table s
我使用 SOCI 连接到数据库库,我想获取数据库故障转移事件以用于日志记录。我不确定要实现什么接口(interface)以及如何进行回调注册。 SOCI有这样的支持吗? 最佳答案 SOCI 唯一支持的
... 并保存成自定义对象类型?我正在使用 PostgreSQL。当我将所有内容都放在一个文件中时,它就可以工作了。但我想把它分成类文件,就像你在用 cpp 编写时所做的那样。当我将我的代码分成 *.
我正在使用 soci 让我的应用程序与数据库交互,目前我使用 MySQL 服务器进行测试。我已将 SOCI 包装到简单的类中,在我类的 Connect 方法中调用 session.open() m_s
目前我正在编写一个 C++ 应用程序,我必须在其中连接到 SQLite 数据库。我搜索图书馆并找到 SOCI,我不得不说:我喜欢它。流语法和映射非常棒。但是我有一个问题: 我有一个 Event 类,我
我扩展了我的类(来自这个问题:How to get a whole row from database using SOCI?)以拥有两个私有(private)成员,还添加了 getter 和 set
以下代码:生成错误消息,如下: bool UACmUsers::GetUser(int userid) { soci::session sql(dbU::Connection());
我使用 soci 库访问 sqlite3 数据库: rowset rs = (sql.prepare get(1); // ... } (这不是我的实际代码;它已大大简化以描述问题。) 现在,
我正在尝试构建 SOCI 库以与 PostgreSQL 一起运行。我遵循了以下步骤: 在 Ubutu 15.10 中安装 PostgreSQL 下载 SOCI 源代码 提取 SOCI 代码 然后我运行
我使用带有 C++ 的 soci 来访问我的数据库。是否可以修改以下表达式以获取赋予该表达式添加的行的新主键? *dbSession << "insert into myTable(myRow) v
我最近为我的项目安装了 SOCI 库,因为它需要使用 SQLite 数据库。我试图获取行集,但出现了奇怪的错误: "c:\mingw\include\soci\exchange-traits.h:35
假设我有两个具有多对多关系的表(即,第三个表仅用于关系)。 SOCI 是否支持语句中不同类型的“连接”? 如果是,它是否适用于所有数据库(在文档中称为后端)? 谢谢! 最佳答案 使用 SOCI,您仍然
我是一名优秀的程序员,十分优秀!