dbTalk Databases Forums  

Returning an object of type Result

mailing.database.mysql-plusplus mailing.database.mysql-plusplus


Discuss Returning an object of type Result in the mailing.database.mysql-plusplus forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Roland Sgorcea
 
Posts: n/a

Default Returning an object of type Result - 05-30-2005 , 06:46 AM






Hi,

Just started using mysql++ today for the first time. I went through
the examples and they worked fine.

I tried to make a generic function=20

Result Select_From_Database ( const char* field_select, const char*
table_name, const char* field_name, const char* field_value)

My setup looks soemthing like this:

Result Select_From_Database ( const char* field_select, const char*
table_name, const char* field_name, const char* field_value)
{
Connection con(use_exceptions);
=09try {
=09=09con.real_connect(MY_DATABASE, MY_HOST, MY_USER, MY_PASSWORD, 3306,
=09=09=09=09=09=09 0, 60, NULL);
=09=09// Create a query object that is bound to con.
=09=09Query query =3D con.query();
=09=09// reset the query object when re-using it.
=09=09query.reset();
=09=09=09=09=09=09
=09=09query << "SELECT " << field_select << " FROM " << table_name << "
WHERE " << field_name << " =3D '" << field_value <<"'";
=09=09=09=09
=09=09Result res =3D query.store();
=09=09=09 =09=09=09=09
=09=09return res;
=09=09=09
=09}
// catch exceptions
}

and I have another function :

char* validate(const char* userlevel, const char* password)=20
{=20
=09Result res =3D Select_From_Database ( "password", "access",
"userlevel", userlevel);
=09
=09=09
=09if ( strcmp(password, res[0][0]) =3D=3D 0 ) {
=09 =09=09=09=09=09=09
=09=09char* access =3D createUserObject(userlevel);=09=09
=09=09=09=09
=09=09return access; // Grants access
=09}
=09else {
=09=09cout << "Login was not Succesfull " << endl;
=09=09return "-1"; // Access NOT GRANTED
=09}=09
}

Something really weird happens, in the Select_From_Database, the res
object size , res.size() is 1 (1 Record found)

but when I pass it on to validate the size becomes 134722808. The
result I'm looking for is still stored in the new res variable
res[0][0] and I can access it fine but after I finish executing the
validate function I get a Segmentation Error and the program shuts
down????

Would really appreciate some help with this !

Thanks in advance,

Roland

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw


Reply With Quote
  #2  
Old   
Chris Frey
 
Posts: n/a

Default Re: Returning an object of type Result - 05-31-2005 , 11:16 AM






On Mon, May 30, 2005 at 11:46:05AM +0000, Roland Sgorcea wrote:
Quote:
Result Select_From_Database ( const char* field_select, const char*
table_name, const char* field_name, const char* field_value)
{
Connection con(use_exceptions);
try {
con.real_connect(MY_DATABASE, MY_HOST, MY_USER, MY_PASSWORD, 3306,
0, 60, NULL);
// Create a query object that is bound to con.
Query query = con.query();
// reset the query object when re-using it.
query.reset();

query << "SELECT " << field_select << " FROM " << table_name << "
WHERE " << field_name << " = '" << field_value <<"'";

Result res = query.store();

return res;
Your connection should live for the entire data-using session. In this
code, connection disappears when the function exits. The result still
depends on it.

You might wish to reorganize your code so that your connection is in main(),
and Select_From_Database takes a reference to it as an argument.

Or organize your database functions in a class, the class containing the
connection.

- Chris


--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #3  
Old   
Warren Young
 
Posts: n/a

Default Re: Returning an object of type Result - 06-01-2005 , 06:35 AM



Roland Sgorcea wrote:

Quote:
Result Select_From_Database ...
In addition to what Chris said, you should consider passing a reference
to the result object to the function, or some other way to prevent this
copy. Result is not a light-weight object...returning it by value is
very wasteful.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.