使用 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