Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
jarchalex is currently offline  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 Go to previous messageGo to next message
jarchalex is currently offline  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 #34277 is a reply to message #34273] Mon, 07 November 2011 20:13 Go to previous messageGo to next message
jarchalex is currently offline  jarchalex
Messages: 5
Registered: October 2011
Promising Member
Now it almost works!

I've made an ugly workaround - copied "Sql/sch_schema.h" to "sql_schema2.h" an changed there "All_Tables" to "All_Tables2".
At least it allows me to create two databases and work with them.

And added NOAPPSQL to build config.

The problem arised in GUI. When i insert record to the first declared table everything works fine, but when i insert record to second table - cursor moves to the first table, and if i click in the empty field there it will create new record in first db, but if i click in empty space in the second table it will create record in the second db (as it should).

Re: How to use multiple schemas and databases? [message #34278 is a reply to message #34277] Mon, 07 November 2011 20:27 Go to previous messageGo to next message
jarchalex is currently offline  jarchalex
Messages: 5
Registered: October 2011
Promising Member
Ok! My mistake was using same EditStrings for two tables. After adding new EditStrings for second table everything works!

Ultimate++ is great!
Re: How to use multiple schemas and databases? [message #34289 is a reply to message #34278] Tue, 08 November 2011 14:23 Go to previous messageGo to next message
jarchalex is currently offline  jarchalex
Messages: 5
Registered: October 2011
Promising Member
Moved "All_Tables()" definition out.

#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"
#include "sql_schema2.h" //All_Tables() commented out
static void All_Tables1(SqlSchema& schema) {
#include SCHEMADIALECT
}
#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_schema2.h"
static void All_Tables2(SqlSchema& schema) {
#include SCHEMADIALECT
}


This way i can use as many databases as application needs. Hopefully.
Re: How to use multiple schemas and databases? [message #45446 is a reply to message #34289] Sun, 15 November 2015 00:44 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
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
#undef ALL_TABLES_NAME



regards
omari.
Re: How to use multiple schemas and databases? [message #45517 is a reply to message #45446] Thu, 26 November 2015 23:55 Go to previous message
sergeynikitin is currently offline  sergeynikitin
Messages: 748
Registered: January 2008
Location: Moscow, Russia
Contributor

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 );
}
Previous Topic: Printer's dialog shows up multiple times
Next Topic: Fossil,SSH,Npackd,GIT and R.
Goto Forum:
  


Current Time: Fri Apr 19 07:14:27 CEST 2024

Total time taken to generate the page: 0.04107 seconds