Working with VS 2012 and Microsoft SQL Server. I got everything to compile and build, but the program dies when I try to persist the first object:
// 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;
}
}
So, I am assuming it's a good thing I got past the create_database part. That means I'm connecting to the server ( I think). The error message I get is:
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.
This is probably something simple, but can't figure it out. Maybe I'm not REALLY getting the access to the db that I need.
Here is 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
OK, I think I figured out what is going on. (It is consistent, in any case). I neglected to create the table in the database via a call like this:
mysql --user=odb_test --database=odb_test < person.sql
This is needed. The database needs to know about the schema before the program can be run. So, my remaining issue is to figure out how to do this in msssql instead of mysql.
UPDATE:
OK, got it to work. I did this by SQL Server Manager, loading the person.sql file and executing the query. It put it in the odb_test database. Then the routine could run.