使用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
答案 0 :(得分:1)
好的,我想我弄清楚发生了什么。 (无论如何,这是一致的)。我忽略了通过这样的调用在数据库中创建表:
mysql --user=odb_test --database=odb_test < person.sql
这是必要的。在运行程序之前,数据库需要了解架构。所以,我剩下的问题是弄清楚如何在msssql而不是mysql中执行此操作。
更新:
好的,让它发挥作用。我通过SQL Server Manager执行此操作,加载person.sql文件并执行查询。它把它放在odb_test数据库中。那么例程就可以运行了。