Discussion:
[odb-users] SQLite not creating table?
Rafal Gm
2013-07-05 07:12:06 UTC
Permalink
Well after I switched from MySQL to SQlite at my surprise there was no
generated .sql file to be used when the following command is issued:

odb -d sqlite -I../Database/libodb-2.2.3 --std c++11 --generate-schema
--generate-query "C:/path_to_file/classes/user.hxx", it generates the
user-odb hxx and cxx files which I include and compile with my project,
then I use this code:

namespace Database
{
odb::sqlite::database * users/* ("accounts.db")*/ = NULL;
};
#define REPORT_ERROR_IF_ANY(e)
Report_Error(e.what(),__FILE__,__FUNCTION__,__LINE__)

void Report_Error(std::string error,std::string file,std::string
function,int line)
{
ServerLog::Printf("Exception ('%s') in
%s::%s::%d",error.c_str(),file.c_str(),function.c_str(),line);
return;
}

main()
{
if(!Database::users)
{
Database::users = new odb::sqlite::database("accounts.db",
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
}
if(Database::users)
{
try
{
user server("SERVER","X",0,0,0,0,0,0,0,0,0);
odb::transaction t (Database::users->begin ());

Database::users->persist (server);

typedef odb::query<user> user_query;

for (user& u: Database::users->query<user> (user_query::nickname ==
"SERVER"))
std::cout << u.nickname().c_str() << std::endl;

server.admin(1337);
Database::users->update (server);

t.commit ();
}
catch(odb::exception &e)
{
REPORT_ERROR_IF_ANY(e);
}
return true;
}

and when I execute this code it give me this:

[08:58:12] Exception ('1: no such table: user') in
exe24.cpp::OnGameModeInit::138

What am I doing wrong?

My user.hxx is this [quite simple file because I'm not that advanced with
ODB :( ]:

#ifdef EXE24_EXPORTS
#pragma once
#endif

#include <string>
#include <vector>
#include <tuple>

#include <odb/core.hxx>

#define acmf(variable_name,variable_type) \
const variable_type& variable_name () const \
{ \
return variable_name ## _; \
} \
void variable_name (variable_type variable_name) \
{ \
variable_name ## _ = variable_name; \
}

#define accessor(variable_name,variable_type) \
const variable_type& variable_name () const \
{ \
return variable_name ## _; \
}

#pragma db value
struct ban_type
{
std::string reason;
unsigned long long date_end;
std::string by_who;
unsigned long long date_when;
};

#pragma db object
class user
{
public:
user () {}
user
(
std::string nickname,
std::string password,
unsigned long long register_date,
unsigned int kills,
unsigned int deaths,
unsigned int joins,
unsigned int disconnects,
unsigned int kicks,
unsigned long long experience,
unsigned long long admin,
unsigned long long banned
):
nickname_(nickname),
password_(password),
register_date_(register_date),
kills_(kills),
deaths_(deaths),
joins_(joins),
disconnects_(disconnects),
kicks_(kicks),
experience_(experience),
admin_(admin),
banned_(banned)
{

}
// Standard accessor/modifier names. Auto-discovered by ODB.
//
acmf(nickname,std::string);
acmf(password,std::string);
acmf(register_date,unsigned long long);
acmf(kills,unsigned int);
acmf(deaths,unsigned int);
acmf(joins,unsigned int);
acmf(disconnects,unsigned int);
acmf(kicks,unsigned int);
acmf(experience,unsigned long long);
acmf(admin,unsigned long long);
acmf(banned,unsigned long long);

accessor(id,unsigned long);
// bans.
// v<t<reason,how_long,by_who,when>>
/*typedef std::tuple<std::string,
unsigned long long,
std::string,
unsigned long long> ban_type;
#pragma db value(ban_type) transient
#pragma db member(ban_type::reason) virtual(std::string) access(std::get<0>
(this))
#pragma db member(ban_type::date_end) virtual(unsigned long long)
access(std::get<1> (this))
#pragma db member(ban_type::by_who) virtual(std::string) access(std::get<2>
(this))
#pragma db member(ban_type::date_when) virtual(unsigned long long)
access(std::get<3> (this))*/

typedef std::vector<ban_type> bans_type;

acmf(bans,bans_type);

private:
friend class odb::access;

#pragma db id auto
unsigned long id_;

std::string nickname_;
std::string password_;
unsigned long long register_date_;
unsigned int kills_;
unsigned int deaths_;
unsigned int joins_;
unsigned int disconnects_;
unsigned int kicks_;
unsigned long long experience_;
unsigned long long admin_;
unsigned long long banned_;

bans_type bans_;
};

Thanks in advance for help!
Boris Kolpackov
2013-07-05 07:39:45 UTC
Permalink
Hi Rafal,
Post by Rafal Gm
Well after I switched from MySQL to SQlite at my surprise there was no
odb -d sqlite -I../Database/libodb-2.2.3 --std c++11 --generate-schema
--generate-query "C:/path_to_file/classes/user.hxx"
For SQLite, by default, the schema is embedded into the generated C++
code so that you can create the tables programmatically from within
your application (this is normally the preferred way when using an
embedded database like SQLite).

If you still want the .sql file, then you can add the following option
to your ODB command line:

--schema-format sql

For more information on how to use embedded schemas, see Section 3.4,
"Database" in the ODB manual.

Boris
Rafal Gm
2013-07-05 08:13:41 UTC
Permalink
Ah indeed that section helped,

odb::transaction x (Database::users->begin ());
odb::schema_catalog::create_schema (*Database::users);
x.commit ();

solved the problem, thanks :)
I preffer programmatically, but I don't see it anywhere in the examples?
(except creating/executing SQL queries :( - not what I want)
Post by Boris Kolpackov
Hi Rafal,
Post by Rafal Gm
Well after I switched from MySQL to SQlite at my surprise there was no
odb -d sqlite -I../Database/libodb-2.2.3 --std c++11 --generate-schema
--generate-query "C:/path_to_file/classes/user.hxx"
For SQLite, by default, the schema is embedded into the generated C++
code so that you can create the tables programmatically from within
your application (this is normally the preferred way when using an
embedded database like SQLite).
If you still want the .sql file, then you can add the following option
--schema-format sql
For more information on how to use embedded schemas, see Section 3.4,
"Database" in the ODB manual.
Boris
Loading...