|
|
Home » Community » Newbie corner » How to use multiple schemas and databases?
How to use multiple schemas and databases? [message #34248] |
Sun, 06 November 2011 17:50  |
jarchalex
Messages: 5 Registered: October 2011
|
Promising Member |
|
|
Hello!
I'm just studying U++, and cannot figure out how to use multiple databases at once (and possibly multiple schemas). I want application to use several dbs with static info and one to actually write project data.
Probably there is something in the way i should include files to define All_Tables() correctly. But how?..
Please help!
N.B.Attached package is for studying purposes only.
Code that does not work:
#include <CtrlLib/CtrlLib.h>
#include <GridCtrl/GridCtrl.h>
#include <Painter/Painter.h>
#include <SqlCtrl/SqlCtrl.h>
#include <plugin/sqlite3/Sqlite3.h>
using namespace Upp;
#define MODEL <studyDraw_01/db.sch>
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
#include "Sql/sch_header.h"
#include "Sql/sch_source.h"
#include "Sql/sch_schema.h"
/*
#define MODEL <studyDraw_01/db_info.sch>
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
#include "Sql/sch_header.h"
#include "Sql/sch_source.h"
#include "Sql/sch_schema.h"
*/
#define MODEL <studyDraw_01/db_info.sch>
//#include "Sql/sch_schema.h"
#define LAYOUTFILE <studyDraw_01/gui.lay>
#include <CtrlCore/lay.h>
struct MyDraw: Ctrl {
virtual void Paint(Draw& w) {
Size sz = GetSize();
ImageBuffer ib(sz);
BufferPainter sw(ib);
DoPaint(sw);
w.DrawImage(0, 0, ib);
}
void DoPaint(Painter& sw) {
Size sz = GetSize();
sw.DrawRect(0, 0, sz.cx, sz.cy, White());
}
};
struct MyWindow: TopWindow {
MenuBar menu;
StatusBar status;
SqlArray table1;
SqlArray tab_settings;
GridCtrl table2;
GridCtrl table3;
TabCtrl tabs;
Splitter spl, set;
EditString name;
EditInt val;
EditDouble valD;
EditString parameter;
EditString value;
MyDraw draw;
Sqlite3Session sqlite3, sqlite3_2;
void InitDB() {
if(!sqlite3.Open(ConfigFile("settings.conf"))) {
Exclamation("Can't create or open database file\n");
return;
}
SQL = sqlite3;
SqlSchema sch(SQLITE3);
sqlite3.SetTrace();
All_Tables(sch);
if(sch.ScriptChanged(SqlSchema::UPGRADE))
Sqlite3PerformScript(sch.Upgrade());
if(sch.ScriptChanged(SqlSchema::ATTRIBUTES))
Sqlite3PerformScript(sch.Attributes());
if(sch.ScriptChanged(SqlSchema::CONFIG)) {
Sqlite3PerformScript(sch.ConfigDrop());
Sqlite3PerformScript(sch.Config());
}
sqlite3.SetTrace();
if(!sqlite3_2.Open(ConfigFile("settings_2.conf"))) {
Exclamation("Can't create or open database file\n");
return;
}
//SQL = sqlite3_2;
SqlSchema sch_2(SQLITE3);
sqlite3_2.SetTrace();
All_Tables(sch_2);
if(sch_2.ScriptChanged(SqlSchema::UPGRADE))
Sqlite3PerformScript(sch_2.Upgrade());
if(sch_2.ScriptChanged(SqlSchema::ATTRIBUTES))
Sqlite3PerformScript(sch_2.Attributes());
if(sch_2.ScriptChanged(SqlSchema::CONFIG)) {
Sqlite3PerformScript(sch_2.ConfigDrop());
Sqlite3PerformScript(sch_2.Config());
}
sqlite3.SetTrace();
}
void Exit() {
if(PromptOKCancel("Exit?"))
Break();
}
void SubMenu(Bar& bar) {
bar.Add("Exit", THISBACK(Exit))
.Help("Exit application");
}
void MainMenu(Bar& bar) {
bar.Add("Menu", THISBACK(SubMenu));
}
typedef MyWindow CLASSNAME;
MyWindow() {
InitDB();
Title("Ïðèìåð ðèñîâàíèÿ");
AddFrame(menu);
AddFrame(status);
table1.SetSession(sqlite3_2);
table1.SetTable(FLOWCONVERSIONUNITS);
table1.AddKey(ID);
table1.AddColumn(UNIT, t_("Units")).Edit(parameter);
table1.AddColumn(FACTOR, t_("Conversion factor")).Edit(value);
table1.Appending().Removing();
table1.SetOrderBy(ID, UNIT);
table2.AddIndex();
table2.AddColumn(0, t_("One"));
table2.AddColumn(t_("Two"));
table2.AddColumn(t_("Three"));
table2.Appending().Removing().Editing().Accepting().Canceling();
table2.RejectNullRow();
table2.SetToolBar();
table3.AddColumn(0, t_("One"));
table3.SetToolBar();
tabs.Add(table1.SizePos(), "table1");
tabs.Add(table2.SizePos(), "table2");
tabs.Add(table3.SizePos(), "table3");
tabs.Set(0);
tab_settings.SetSession(sqlite3);
tab_settings.SetTable(SETTINGS);
tab_settings.AddKey(ID);
tab_settings.AddColumn(PARAMETER, t_("Parameter")).Edit(parameter);
tab_settings.AddColumn(VALUE, t_("Value")).Edit(value);
tab_settings.Appending().Removing();
tab_settings.SetOrderBy(ID, PARAMETER);
spl.Vert();
spl.Add(tab_settings);
spl.Add(draw);
spl.Add(tabs);
spl.SetPos(0,0);
Add(spl);
menu.Set(THISBACK(MainMenu));
menu.WhenHelp = status;
tab_settings.Query();
}
};
GUI_APP_MAIN
{
MyWindow w;
w.Sizeable().MinimizeBox().MaximizeBox();
w.SetRect(0, 0, 600, 500);
w.Run();
}
|
|
|
Re: How to use multiple schemas and databases? [message #34273 is a reply to message #34248] |
Mon, 07 November 2011 17:39   |
jarchalex
Messages: 5 Registered: October 2011
|
Promising Member |
|
|
Now this code is already compilable )) But causes SQL error "no such table: FLOWCONVERSIONUNITS"
#include <CtrlLib/CtrlLib.h>
#include <GridCtrl/GridCtrl.h>
#include <Painter/Painter.h>
#include <SqlCtrl/SqlCtrl.h>
#include <plugin/sqlite3/Sqlite3.h>
using namespace Upp;
#define MODEL <studyDraw_01/db.sch>
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
#include "Sql/sch_header.h"
#include "Sql/sch_schema.h"
#include "Sql/sch_source.h"
#define LAYOUTFILE <studyDraw_01/gui.lay>
#include <CtrlCore/lay.h>
struct Tab_Settings {
Sqlite3Session sqlite3;
Tab_Settings() { Init();}
void Init() {
if(!sqlite3.Open(ConfigFile("settings.conf"))) {
Exclamation("Can't create or open database file\n");
return;
}
SQL = sqlite3;
SqlSchema sch(SQLITE3);
sqlite3.SetTrace();
All_Tables(sch);
if(sch.ScriptChanged(SqlSchema::UPGRADE))
Sqlite3PerformScript(sch.Upgrade());
if(sch.ScriptChanged(SqlSchema::ATTRIBUTES))
Sqlite3PerformScript(sch.Attributes());
if(sch.ScriptChanged(SqlSchema::CONFIG)) {
Sqlite3PerformScript(sch.ConfigDrop());
Sqlite3PerformScript(sch.Config());
}
sqlite3.SetTrace();
}
};
#define MODEL <studyDraw_01/db_info.sch>
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
#include "Sql/sch_header.h"
#include "Sql/sch_source.h"
struct Tab_FlowConversionUnits {
Sqlite3Session sqlite3;
Tab_FlowConversionUnits() { Init(); }
void Init() {
if(!sqlite3.Open(ConfigFile("settings_2.conf"))) {
Exclamation("Can't create or open database file\n");
return;
}
SqlSchema sch(SQLITE3);
sqlite3.SetTrace();
All_Tables(sch);
if(sch.ScriptChanged(SqlSchema::UPGRADE))
Sqlite3PerformScript(sch.Upgrade());
if(sch.ScriptChanged(SqlSchema::ATTRIBUTES))
Sqlite3PerformScript(sch.Attributes());
if(sch.ScriptChanged(SqlSchema::CONFIG)) {
Sqlite3PerformScript(sch.ConfigDrop());
Sqlite3PerformScript(sch.Config());
}
sqlite3.SetTrace();
}
};
struct MyDraw: Ctrl {
virtual void Paint(Draw& w) {
Size sz = GetSize();
ImageBuffer ib(sz);
BufferPainter sw(ib);
DoPaint(sw);
w.DrawImage(0, 0, ib);
}
void DoPaint(Painter& sw) {
Size sz = GetSize();
sw.DrawRect(0, 0, sz.cx, sz.cy, White());
sw.Rectangle(sz.cx*0.05,sz.cy*0.05,sz.cx*0.9,sz.cy*0.9);
sw.Fill(LtBlue());
sw.Scale(0.5);
sw.Translate(0, 50);
const char *txt = "GRADIENT TEXT";
Font fnt = Arial(100).Bold();
Size tsz = GetTextSize(txt, fnt);
sw.Text(100, 100, txt, fnt)
.Stroke(4, 100, 100, Blue(), 100 + tsz.cx, 100, LtRed());
}
void RightDown(Point, dword) {
CallbackArgTarget<int> result;
MenuBar rMenu;
for(int i = 0; i < 10; i++)
rMenu.Add(AsString(i), result[i]);
rMenu.Execute();
if(!IsNull(result))
PromptOK("You have selected " + AsString((int)result));
}
};
struct MyWindow: TopWindow {
MenuBar menu;
StatusBar status;
SqlArray table1;
SqlArray tab_settings;
GridCtrl table2;
GridCtrl table3;
TabCtrl tabs;
Splitter spl, set;
EditString name;
EditInt val;
EditDouble valD;
EditString parameter;
EditString value;
MyDraw draw;
Tab_Settings tab_set;
Tab_FlowConversionUnits tab_flow;
void Exit() {
if(PromptOKCancel("Exit?"))
Break();
}
void SubMenu(Bar& bar) {
bar.Add("Exit", THISBACK(Exit))
.Help("Exit application");
}
void MainMenu(Bar& bar) {
bar.Add("Menu", THISBACK(SubMenu));
}
typedef MyWindow CLASSNAME;
MyWindow() {
Title("DB test");
AddFrame(menu);
AddFrame(status);
table1.SetSession(tab_flow.sqlite3);
table1.SetTable(FLOWCONVERSIONUNITS);
table1.AddKey(ID);
table1.AddColumn(UNIT, t_("Units")).Edit(parameter);
table1.AddColumn(FACTOR, t_("Conversion factor")).Edit(value);
table1.Appending().Removing();
table1.SetOrderBy(ID, UNIT);
table2.AddIndex();
table2.AddColumn(0, t_("One"));
table2.AddColumn(t_("Two"));
table2.AddColumn(t_("Three"));
table2.Appending().Removing().Editing().Accepting().Canceling();
table2.RejectNullRow();
table2.SetToolBar();
table3.AddColumn(0, t_("One"));
table3.SetToolBar();
tabs.Add(table1.SizePos(), "table1");
tabs.Add(table2.SizePos(), "table2");
tabs.Add(table3.SizePos(), "table3");
tabs.Set(0);
tab_settings.SetSession(tab_set.sqlite3);
tab_settings.SetTable(SETTINGS);
tab_settings.AddKey(ID);
tab_settings.AddColumn(PARAMETER, t_("Parameter")).Edit(parameter);
tab_settings.AddColumn(VALUE, t_("Value")).Edit(value);
tab_settings.Appending().Removing();
tab_settings.SetOrderBy(ID, PARAMETER);
spl.Vert();
spl.Add(tab_settings);
spl.Add(draw);
spl.Add(tabs);
spl.SetPos(0,0);
Add(spl);
menu.Set(THISBACK(MainMenu));
menu.WhenHelp = status;
tab_settings.Query();
}
};
GUI_APP_MAIN
{
MyWindow w;
w.Sizeable().MinimizeBox().MaximizeBox();
w.SetRect(0, 0, 600, 500);
w.Run();
}
|
|
|
|
|
|
Re: How to use multiple schemas and databases? [message #45446 is a reply to message #34289] |
Sun, 15 November 2015 00:44   |
omari
Messages: 276 Registered: March 2010
|
Experienced Member |
|
|
Interesting,
i think that can be acheived by adding a macro ALL_TABLES_NAME :
#define MODEL <studyDraw_01/db.sch>
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
#include "Sql/sch_header.h"
#include "Sql/sch_source.h"
#include "Sql/sch_schema.h"
#define MODEL <studyDraw_01/db_info.sch>
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
#include "Sql/sch_header.h"
#include "Sql/sch_source.h"
#define ALL_TABLES_NAME AllTables2
#include "Sql/sch_schema.h"
then in the file "Sql/sch_schema.h" :
- use ALL_TABLES_NAME instead of AllTables
- add this macro befor :
#ifndef ALL_TABLES_NAME
#define ALL_TABLES_NAME AllTables
#endif
- add this one at the end of file
regards
omari.
|
|
|
Re: How to use multiple schemas and databases? [message #45517 is a reply to message #45446] |
Thu, 26 November 2015 23:55  |
|
Something like this:
#define NOAPPSQL
...
#include <plugin/sqlite3/Sqlite3.h>
#include <Sql/sch_schema.h>
Sqlite3Session db1;
Sqlite3Session db2;
{
#define MODEL <MySuperApp/db1.sch>
#include "Sql/sch_source.h"
db1.Open("db1.db");
SqlSchema sch1(SQLITE3);
All_Tables(sch1);
sch1.SaveNormal();
SqlPerformScript(sch1.Upgrade());
SqlPerformScript(sch1.Attributes());
}
#undef MODEL
{
#define MODEL <MySuperApp/db2.sch>
#include "Sql/sch_source.h"
db2.Open("db2.db");
SqlSchema sch2(SQLITE3);
All_Tables(sch2);
sch2.SaveNormal();
SqlPerformScript(sch2.Upgrade());
SqlPerformScript(sch2.Attributes());
}
...
Sql sql1(db1);
sql1 * Select .....
Sql sql2(db2);
sql2 * Insert .....
SergeyNikitin<U++>( linux, wine )
{
    under( Ubuntu || Debian || Raspbian );
}
|
|
|
Goto Forum:
Current Time: Mon Apr 28 14:26:27 CEST 2025
Total time taken to generate the page: 0.00529 seconds
|
|
|