List:MySQL++« Previous MessageNext Message »
From:Chris Frey Date:May 30 2005 5:10pm
Subject:Re: Returning an object of type Result
View as plain text  
On Mon, May 30, 2005 at 11:46:05AM +0000, Roland Sgorcea wrote:
> 
> 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

Thread
Returning an object of type ResultRoland Sgorcea30 May
  • Re: Returning an object of type ResultChris Frey31 May
  • Re: Returning an object of type ResultWarren Young1 Jun