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 » U++ Library support » U++ SQL » Sql global
Sql global [message #6598] Mon, 20 November 2006 08:47 Go to next message
laurice is currently offline  laurice
Messages: 22
Registered: September 2006
Promising Member
How do you declare a global Sql sql instance?
Why do I need this? I want to speed up a very long process by executing just one query and my SetTimeCallback function will just fetch it from the global sql.Fetch().

I tried to declare a global Sql sql and attach the session by
sql = Sql(session), but it exits the program.

Are there any other approach other than populating global Vectors or Arrays, such as Sql iterators?

Thanks,

Laurice
Re: Sql global [message #6599 is a reply to message #6598] Mon, 20 November 2006 09:50 Go to previous messageGo to next message
pepe11 is currently offline  pepe11
Messages: 16
Registered: June 2006
Location: Slovakia
Promising Member

Hi,
In Sqls.h is on line 211 :

struct AppSql : Sql {
void operator=(SqlSource& s) { Assign(s); }
AppSql() : Sql(NULLSQL) {}
};
AppSql& AppCursor();
#define SQL AppCursor()

And in Sql.cpp is written on line 404 :
GLOBAL_VAR(AppSql, AppCursor)

Try used SQL as global instance.
It is only my observant. Maybe i'm wrong.

pepe11
Re: Sql global [message #6604 is a reply to message #6599] Mon, 20 November 2006 11:24 Go to previous messageGo to next message
laurice is currently offline  laurice
Messages: 22
Registered: September 2006
Promising Member
Can you give a short code snippet on how can I use this SQL macro?

Re: Sql global [message #6605 is a reply to message #6604] Mon, 20 November 2006 12:03 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 697
Registered: December 2005
Location: Budapest, Hungary
Contributor
I'm using something like these two functions to open and close sqlite database file:
void OpenDB()
{
	Sqlite3Session &sql_session = Single<Sqlite3Session>();
#ifdef _DEBUG
	sql_session.LogErrors(true);
#endif
	if(!sql_session.Open(ConfigFile("data.db"))) {
		LOG("Can't create or open database file\n");
		return;
	}
	
	// This is global:
	SQL = sql_session;
}

void CloseDB()
{
	Single<Sqlite3Session>().Close();
}


And in code I query database this way:
	Sql sql;
	sql * Select(SqlAll()).From(SOME_TABLE);
	while(sql.Fetch()){
		//Do something
		Value some_field = sql[SOME_FIELD];
	}
Re: Sql global [message #6617 is a reply to message #6605] Mon, 20 November 2006 22:04 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
zsolt wrote on Mon, 20 November 2006 06:03

I'm using something like these two functions to open and close sqlite database file:



Interesting, why two functions and Single?

In my apps, I open DB in GUI_APP_MAIN and let destructor to close it.

GUI_APP_MAIN {
   Sqlite3Session s;
   if(!s.Open(...)) ...
   SQL = s;

   ..run the app...
}


Mirek
Re: Sql global [message #6618 is a reply to message #6617] Mon, 20 November 2006 23:06 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 697
Registered: December 2005
Location: Budapest, Hungary
Contributor
Quote:

Interesting, why two functions and Single?

I implemented this, because in my app, I have to close and open databases while the app is running. But SqlSession interface, that I can get from the global SQL, has no Open() method to open sqlite3 session.
Re: Sql global [message #6619 is a reply to message #6618] Mon, 20 November 2006 23:11 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
That explains it... (but in that case, I may rather used Sqlite variable instead of Single and just Open - it should close the previous connection first, at least that is how U++ classes are expected to work....).

Mirek
Re: Sql global [message #6621 is a reply to message #6619] Mon, 20 November 2006 23:26 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 697
Registered: December 2005
Location: Budapest, Hungary
Contributor
luzr wrote on Mon, 20 November 2006 23:11

That explains it... (but in that case, I may rather used Sqlite variable instead of Single and just Open - it should close the previous connection first, at least that is how U++ classes are expected to work....).

I don't undestand this. I use Single, because the session have to be live in memory. If I create an Sqlitee3Session on the stack and Open() it, it will be destroyed at the end of the method.
Or do you mean, that using a global Sqlitee3Session is better than single?
Re: Sql global [message #6623 is a reply to message #6621] Tue, 21 November 2006 01:19 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Quote:


Or do you mean, that using a global Sqlitee3Session is better than single?


Sometimes. But I rather had in mind member variable.

Anyway, solution with Single is OK as well. Just that Sqlite is not the typical singleton class Wink

Mirek
Re: Sql global [message #6626 is a reply to message #6598] Tue, 21 November 2006 05:37 Go to previous message
laurice is currently offline  laurice
Messages: 22
Registered: September 2006
Promising Member
great, modified the source code with SQL, and works like a charm. Smile
Previous Topic: plugin/SQlite still wrong naming for linux
Next Topic: mysql error
Goto Forum:
  


Current Time: Sat Apr 27 20:28:20 CEST 2024

Total time taken to generate the page: 0.06609 seconds