List:MySQL++« Previous MessageNext Message »
From:Earl Miles Date:November 17 2004 5:51pm
Subject:Re: How to write Mysql++ in OOP
View as plain text  
Hi, Dovid. I think I may see what's wrong here, which is that you're 
accidentally not using the Connection object you declared in the class, 
but are creating a local override. So your guess as to what you're doing 
wrong is basically correct.

(Comments down with the code)



Dovid Kopel wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> I finally fixed the segfaults I was having, though the only way I can
> execute a query is by having the Query object in the same method as the
> Connection object. I declared the Connection as private in my header but
> when I try to run the "query.store()" I get "Error: MySQL server has
> gone away" which sounds like a problem with the connection. Forgive me,
> I am a PHP programmer who has Java experience. I'm teaching myself Java
> to write a db app. I'm probably doing a declaration wrong or something
> at a simple level. Thank you in advance.
> 
> #include "mysql.h"
> #include <mysql++.h>
> #include <iomanip>
> 
> using namespace std;
> using namespace mysqlpp;
> database::database(const char *db_db, const char *db_host, const char
> *db_user, const char *db_passwd)
> {
>     try
>     {
>         Connection con(db_db, db_host, db_user, db_passwd);
>         Query query = con.query();
>         query << "SHOW TABLES";
>         cout << "Query: " << query.preview() << endl;
>         Result res = query.store();
>         cout << "Records Found: " << res.size() << endl;
>     }
>     catch (BadQuery& er)
>     {
>         cerr << "Error: " << er.error << endl;
>     }
> }
> 
> int database::connect(const char *db_db, const char *db_host, const char
> *db_user, const char *db_passwd)
> {
>     try
>     {
>         Connection con(db_db, db_host, db_user, db_passwd);
The above line declares a Connection object on the stack. A soon
as it hits this:
>     }
That connection object is destroyed again. If you have Connection con
in the class definition for database, this code is not opening that
connection. You probably want to replace the above line with:
	con.connect(db_db, db_host, db_user, db_passwd);
>     catch (BadQuery& er)
>     {
>         cerr << "Error: " << er.error << endl;
>     }
>     cout << "Connected..." << endl;
>     return(0);
> }
> 
> int database::exquery(const char *myquery)
> {
>     try
>     {
>         Query query = con.query();
>         query << myquery;
>         cout << "Query: " << query.preview() << endl;
>         Result res = query.store();
>         cout << "Records Found: " << res.size() << endl;
>     }
>     catch (BadQuery& er)
>     { // handle any connection or
> ~                          // query errors that may come up
>         cerr << "Error: " << er.error << endl;
>         return -1;
>     }
>     return(0);
> }

Thread
How to write Mysql++ in OOPDovid Kopel17 Nov
  • Re: How to write Mysql++ in OOPEarl Miles17 Nov
  • Re: How to write Mysql++ in OOPWarren Young18 Nov