3

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
4

1 に答える 1