ultimatepp/reference/SQL_MultiDB/main.cpp
cxl 69b774a57e .reference
git-svn-id: svn://ultimatepp.org/upp/trunk@14457 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2020-05-12 13:30:53 +00:00

55 lines
1.5 KiB
C++

#include <MySql/MySql.h>
using namespace Upp;
#define SCHEMADIALECT <MySql/MySqlSchema.h>
#define MODEL <SQL_MultiDB/mysql_schema.sch>
#include <Sql/sch_source.h> // creates the code for mapping schema structures to database rows
#include <Sql/sch_schema.h> // creates the code to generate sql scripts that upload schema to DB
#undef SCHEMADIALECT
namespace DB2 {
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
#define MODEL <SQL_MultiDB/sqlite3_schema.sch>
#include <Sql/sch_source.h>
#include <Sql/sch_schema.h>
#undef SCHEMADIALECT
}
CONSOLE_APP_MAIN
{
Sqlite3Session sqlite3;
MySqlSession mysql;
// edit the connection parameters if necessary
if(mysql.Connect("root", "Passw0rd", "test")) {
Cout() << "Connected\n";
SQL = mysql;
SqlSchema sch(MY_SQL);
All_Tables(sch);
// create the table if necessary
SqlPerformScript(sch.Upgrade());
SqlPerformScript(sch.Attributes());
SQL.ClearError();
try {
// insert some random data
SQL & Insert(TEST_TABLE)(VALUE, Uuid::Create().ToString());
// fetch some data
Sql sql;
sql * Select(ID, VALUE).From(TEST_TABLE)
.OrderBy(Descending(ID))
.Limit(5);
while(sql.Fetch())
Cout() << AsString(sql[0]) << ": " << AsString(sql[VALUE]) << "\n";
}
catch(SqlExc &ex) {
Cerr() << "ERROR: " << ex << "\n";
SetExitCode(1);
}
}
else {
Cerr() <<"ERROR: Unable to connect to database\n";
SetExitCode(1);
}
}