On Sep 11, 2008, at 3:21 AM, James Vanns wrote:
> OK attached is the code used with release 2.3.2 which worked.
Comments inline below.
> I do not initialise the Query object - I use the extraction operator:
That would be the insertion operator. But no, this should not be the
cause. There are some new ways to initialize a Query object in v3
relative to v2, but the old ones still work.
> // MySQL headers
> #include <mysql/mysql.h>
> #include <mysql/errmsg.h>
Take these out. MySQL++ pulls in all needed MySQL C API headers for
you when you...
> // MySQL++ headers
> #include <mysql++/query.h>
> #include <mysql++/ssqls.h>
> #include <mysql++/sql_types.h>
> #include <mysql++/exceptions.h>
> #include <mysql++/connection.h>
...include <mysql++/mysql++.h> instead. The only other header end-
user programs should include directly is ssqls.h, and that's because
it's an optional feature of MySQL++. If for some reason you do need a
MySQL header that MySQL++ doesn't pull in for you, you should include
it after these other two. That ensures that MySQL++ gets things set up
the way it needs them to be.
On the subject of header inclusion, I would also flip your current
include order around. I don't know that this will fix your current
problem, but I've found that it avoids a lot of other problems,
especially dependency ones. If it does fix it, it would be due to the
flipping of the MySQL++ and MySQL header inclusion order. MySQL++ is
pretty picky about how it interfaces with the C API.
The order I've arrived at after long practice is:
- the module's own header. If the module doesn't need anything
declared in it, do it anyway, to make the compiler verify that
declarations match definitions. (Don't tell me you've never changed a
function definition and forgot to update the prototype in the header!)
- other headers from the same program
- headers for convenience libraries associated with the program
(common facilities used across several of your programs, but which
aren't shared outside your organization)
- headers for third-party libraries. Often there are sub-groups
within this one, again, in order from less generic to more generic.
This is why MySQL headers come after MySQL++ ones.
- operating system headers (unistd.h, windows.h, sys/*.h, etc.)
- C++ standard library headers
- C standard library headers
Within each group, I almost always just use alphabetical order. It's
rare that there is a dependency problem within a group that forces
some other order. The exceptions are mainly when I want a sub-group
for organization reasons. For instance, you generally need a small
handful of OS headers to do networking on *ix, so I group these
separate from the others.
If you want to see examples of this practice, see the MySQL++ source,
including the examples and the test suite. I converted it to this
style long ago. At the time MySQL++ had a horrible header
interdependency problem.
> } catch (const ConnectionFailed &p_exception) {
> cerr << "Failed to connect to database." << endl;
> } catch (const BadQuery &p_exception) {
> switch (con.errnum ()) {
> case CR_SERVER_LOST:
> case CR_CONN_HOST_ERROR:
> case CR_CONNECTION_ERROR:
> case CR_SERVER_GONE_ERROR:
> cerr << "Database disconnected us." << endl;
> break;
> default:
> cerr << "MySQL spat out this error: " <<
> p_exception.what () << endl;
> break;
> }
> }
I'd also catch mysqlpp::Exception or std::exception, just in case.
Other than that, the code looks fine.
Are all 2.3.2-related files cleared off the machine? I don't expect
that your program would even build if you were mixing header and
library versions, but to be sure, I only keep one version installed on
any given machine.
Have you completely rebuilt the program against v3, not relying on
your build system's calculated dependencies?
Have you tried it on a different machine?
Have you tried running the program with just one query-and-print
operation, instead of all three?
I've modified one of the examples to be even closer to your style of
code, and it still works:
Index: examples/ssqls1.cpp
===================================================================
--- examples/ssqls1.cpp (revision 2355)
+++ examples/ssqls1.cpp (working copy)
@@ -31,7 +31,7 @@
#include "stock.h"
#include <iostream>
-#include <vector>
+#include <list>
using namespace std;
@@ -49,16 +49,18 @@
mysqlpp::Connection con(db, server, user, pass);
// Retrieve a subset of the stock table's columns, and store
- // the data in a vector of 'stock' SSQLS structures. See the
+ // the data in a list of 'stock' SSQLS structures. See the
// user manual for the consequences arising from this quiet
// ability to store a subset of the table in the stock SSQLS.
- mysqlpp::Query query = con.query("select item,description from
stock");
- vector<stock> res;
+ const string qstr("select item,description from stock");
+ mysqlpp::Query query = con.query();
+ query << qstr;
+ list<stock> res;
query.storein(res);
// Display the items
cout << "We have:" << endl;
- vector<stock>::iterator it;
+ list<stock>::const_iterator it;
for (it = res.begin(); it != res.end(); ++it) {
cout << '\t' << it->item;
if (it->description != mysqlpp::null) {