[odb-users] Separate database schema. "Unknown database" error
Роман Куксин
rkuksin91 at mail.ru
Thu Oct 3 06:39:27 EDT 2013
Hello!
Sorry for my English.
I'm trying to create several sqlite files with different database schemas.
In this example class one should be stored in 1.db file whith one schema. Class two should be stored similary.
----------------------------
header.h:
----------------------------
#ifndef HEADER_H
#define HEADER_H
#include <odb/core.hxx>
//1.db file data
#pragma db object schema("one")
class one
{
public:
#pragma db id auto
int id;
float a,b,c;
};
//2.db file data
#pragma db object schema("two")
class two
{
public:
#pragma db id auto
int id;
int a,b,c;
};
#endif
----------------------------
database.h:
----------------------------
#ifndef DATABASE_H
#define DATABASE_H
#include<iostream>
#include <odb/database.hxx>
#include <odb/connection.hxx>
#include <odb/transaction.hxx>
#include <odb/schema-catalog.hxx>
#include <odb/sqlite/database.hxx>
inline std::shared_ptr<odb::database>
create_database (std::string filename, std::string schemaName = "")
{
using namespace std;
using namespace odb::core;
shared_ptr<database> db (
new odb::sqlite::database( filename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
{
connection_ptr c (db->connection ());
c->execute ("PRAGMA foreign_keys=OFF");
transaction t (c->begin ());
schema_catalog::create_schema (*db, schemaName);
t.commit ();
c->execute ("PRAGMA foreign_keys=ON");
}
return db;
}
#endif
----------------------------
main.cpp:
----------------------------
#include "database.h"
#include "header.h"
#include "header-odb.hxx"
int main()
{
try
{
auto db1 = create_database("1.db", "one");
auto db2 = create_database("2.db", "two");
{
odb::transaction t(db1->begin());
db1->persist(one());
t.commit();
}
odb::transaction t(db2->begin());
db2->persist(two());
t.commit();
}
catch( odb::database_exception & e )
{
std::cout << e.what()<<std::endl;
}
system("pause");
};
----------------------------
odb compiller options:
----------------------------
odb.exe --std c++11 --database sqlite --generate-query --generate-schema --schema-name one --schema-name two --default-pointer std::shared_ptr header.h
----------------------------
As result I have got following output:
1:unknown database "one"
What's wrong with it?
I use Visual Studio 2012 with latest odb compiler and runtime libraries.
------------------------------------------------------------------------------------------------------
С уважением. Роман Куксин.
More information about the odb-users
mailing list