使用 VS 2012 和 Microsoft SQL Server。 我得到了所有要编译和构建的东西,但是当我尝试保留第一个对象时程序就死了:
// file : hello/driver.cxx
// copyright : not copyrighted - public domain
#include <memory> // std::auto_ptr
#include <iostream>
#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include "database.hxx" // create_database
#include "person.hxx"
#include "person-odb.hxx"
using namespace std;
using namespace odb::core;
int
main (int argc, char* argv[])
{
try
{
auto_ptr<database> db (create_database (argc, argv));
unsigned long john_id, joe_id;
// Create a few persistent person objects.
//
{
person john ("John", "Doe", 33);
person jane ("Jane", "Doe", 32);
person joe ("Joe", "Dirt", 30);
transaction t (db->begin ());
// Make objects persistent and save their ids for later use.
//
john_id = db->persist (john); // Note: dies here
db->persist(jane);
joe_id = db->persist(joe);
t.commit ();
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
因此,我假设我通过了 create_database 部分是件好事。这意味着我正在连接到服务器(我认为)。我收到的错误消息是:
208 (42S02) [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'person'.
8180 (42000) [Microsoft][SQL Server Native Client 11.0][SQL Server]Statement(s) could not be prepared.
这可能很简单,但无法弄清楚。也许我并没有真正获得对我需要的数据库的访问权。
这是 person.hxx:
// file : hello/person.hxx
// copyright : not copyrighted - public domain
#ifndef PERSON_HXX
#define PERSON_HXX
#include <string>
#include <cstddef> // std::size_t
#include <odb/core.hxx>
#pragma db object
class person
{
public:
person (const std::string& first,
const std::string& last,
unsigned short age)
: first_ (first), last_ (last), age_ (age)
{
}
const std::string&
first () const
{
return first_;
}
const std::string&
last () const
{
return last_;
}
unsigned short
age () const
{
return age_;
}
void
age (unsigned short age)
{
age_ = age;
}
private:
friend class odb::access;
person () {}
#pragma db id auto
unsigned long id_;
std::string first_;
std::string last_;
unsigned short age_;
};
#pragma db view object(person)
struct person_stat
{
#pragma db column("count(" + person::id_ + ")")
std::size_t count;
#pragma db column("min(" + person::age_ + ")")
unsigned short min_age;
#pragma db column("max(" + person::age_ + ")")
unsigned short max_age;
};
#endif // PERSON_HXX
最佳答案
好的,我想我知道发生了什么。 (无论如何,它是一致的)。我忽略了通过这样的调用在数据库中创建表:
mysql --user=odb_test --database=odb_test < person.sql
这是必需的。在程序运行之前,数据库需要知道模式。所以,我剩下的问题是弄清楚如何在 msssql 而不是 mysql 中执行此操作。
更新:
好的,开始工作了。我通过 SQL Server 管理器执行此操作,加载 person.sql 文件并执行查询。它把它放在 odb_test 数据库中。然后例程可以运行。
关于c++ - 试图让简单的 ODB 'hello' 程序工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24333971/