|
|
Home » U++ Library support » U++ SQL » sql session in a global variable?
sql session in a global variable? [message #34831] |
Tue, 13 December 2011 22:55  |
Wolfgang
Messages: 146 Registered: November 2011 Location: Germany
|
Experienced Member |
|
|
Hi,
I have a method in which my sqlite session is being connected:
.h
class sqltry
{
public:
typedef sqltry CLASSNAME;
sqltry();
private:
Sqlite3Session sqlite3;
Sql sqlPointer;
.cpp
bool sqltry::connect(String path)
{
if (!sqlite3.Open(path))
{
return false;
}
Sql sql(sqlite3);
sqlPointer = sql;
}
But if i try to use the sqlPointer in another method of the same class my app stops working and crashes...
.cpp
bool sqltry::atest(S)
{
if (sqlPointer.Execute("create table t2 (t1key INTEGER PRIMARY KEY,data TEXT,num double,timeEnter DATE);"))
PromptOK("DONE!");
return true;
}
Whats wrong with it?
|
|
|
Re: sql session in a global variable? [message #34839 is a reply to message #34831] |
Wed, 14 December 2011 10:29   |
mr_ped
Messages: 826 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
Didn't try the sources, so just by quick reading it...
The "sql" in connect procedure is local variable, so it gets destroyed once it gets out of scope. I'm afraid this is making it's content invalid. The assignment to "sqlPointer", unless the Sql class has very clever copy constructor, will not help to preserve it. I'm too lazy to check actual Sql source, sorry. 
Anyway, I don't like the "sqlPointer" variable name, because it is not pointer, but an object, so the variable name is misleading.
I would suggest to completely remove sqlPointer class member variable, and create new instance of Sql every time you need it.
See http://www.ultimatepp.org/srcdoc$Sql$tutorial$en-us.html , especially step 2 may help you. After you set global "SQL", you can then get new instance by simple "Sql sql;" in your code.
|
|
|
|
Re: sql session in a global variable? [message #34842 is a reply to message #34831] |
Wed, 14 December 2011 11:03   |
mr_ped
Messages: 826 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
well, after reading that tutorial myself I think your first version may work if you change connect:
bool sqltry::connect(String path)
{
if (!sqlite3.Open(path))
{
return false;
}
sqlPointer = sqlite3;
}
And of course rename that sqlPointer to something more verbose, like "someDatabaseForThisAndThatSql", so when you read it two years later, you will get idea what is it good for.
(to get pointers you would need to start to use "*", "&" and new/delete where appropriate. I don't believe you want to do that in this case. 
edit - addition:
you can of course use fresh instances as needed even with multiple databases. I would maybe put the most used one into SQL, so then Sql instances for that one would be "Sql sql;", Sql instances for other databases would be created like "Sql sql(other_db_session);"
[Updated on: Wed, 14 December 2011 11:08] Report message to a moderator
|
|
|
|
Re: sql session in a global variable? [message #34858 is a reply to message #34854] |
Thu, 15 December 2011 09:51   |
Wolfgang
Messages: 146 Registered: November 2011 Location: Germany
|
Experienced Member |
|
|
I see what u mean but upp tells me
Quote: | e:\ultimate++\myapps\urlaub\SQL.h(25) : error C2091: function returns function
e:\ultimate++\myapps\urlaub\SQL.h(27) : error C2440: 'return' : cannot convert from 'Upp::Sql' to 'Upp::Sql &(__cdecl *)(void)'
|
Sqlite3Session sqlSession;
One<Sql> sql;
Sql& SQL()
{
if(!sql) sql = new Sql(sqlSession);
return *sql;
}
Line 27 is return *sql;
[Updated on: Thu, 15 December 2011 09:54] Report message to a moderator
|
|
|
|
Re: sql session in a global variable? [message #34861 is a reply to message #34831] |
Thu, 15 December 2011 11:26   |
Wolfgang
Messages: 146 Registered: November 2011 Location: Germany
|
Experienced Member |
|
|
ok... but after all the thinking about a good design for such a sqlite class i decided not to write one. The use of sqlite is such simple with the existing sqlite class in upp that there is no need for an additional class. 
Sorry for the trouble i caused!
[Updated on: Thu, 15 December 2011 11:26] Report message to a moderator
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri May 09 22:33:32 CEST 2025
Total time taken to generate the page: 0.01664 seconds
|
|
|