MySQL Lists are EOL. Please join:

List:MySQL++« Previous MessageNext Message »
From:Warren Young Date:January 15 2013 10:28pm
Subject:Re: MySQL++ Query Loop : Commands Out of Sync?
View as plain text  
On 1/15/2013 14:23, Jordan Hewitt wrote:
> Here is my code:

Thanks for sharing.  (Truly!)  From this I can see you're quite 
confused.  Let's try and fix that.

> query.execute();
> ...
> query.reset();
> ...
> while(query.more_results())

Query::more_results() should only be used with multiqueries or use() 
queries, neither of which I see here.

If you study examples/resetdb.cpp, you'll see that it execute()s the 
CREATE TABLE query, and that's it.  No reset(), no more_results() loop.

>                 // Technically, I should need the lines below, since,
>                 // according to the MySQL++ documentation on Query.reset():
>                 //      As of v3.0, Query objects auto-reset upon query execution
> unless
>                 //      you've set it up for making template queries.

How exactly does the conclusion follow?  You aren't using template 
queries, so why do you believe you need to manually reset the Query object?

> MySQL returns with: Commands out of sync;

That error means you've asked the MySQL C API library to do something 
else while it is still in the middle of dealing with the previous thing 
you asked it to do.  The C API library can do only one thing per thread; 
you have to completely finish any operation before it can move on and do 
something else.

You've made an execute() query here, which means there *can be nothing* 
else for the C API to do.  execute() queries are for fire-and-forget 
things, like CREATE TABLE.  They succeed or they do not; end of story.

So, I believe the problem is your more_results() loop, which is asking 
the C API library to check for more results on a query it thinks is 
already finished.

> For some reason MySQL++ thinks I'm
> running the queries simultaneously.

MySQL++ thinks nothing of the kind.  It is simply telling you what the 
underlying C API library and/or the database think.

> Even if I do the following, I still get the error:
>    * Replace remove lines19-21
>    * After the if(!tableExists) {...} block, add the following lines:
>      query = NULL;
>      delete query;
>      query = connection.query();

You must be new to C++.  You've managed to make about one error per line 
of code here.

1. "query" isn't a pointer, so setting it to NULL won't do anything you 
want.  The only reason it even compiles is that Query::operator=() takes 
a Query reference, and Query can be constructed from a null pointer 
(Connector*).  Using that query object will probably crash the program. 
  It certainly won't do anything useful.

2. "delete query" is wrong for essentially the same reasons.  The 
difference is that the conversion path that allows it to compile is 
Query's truthiness operator.

3. Even if "query" were a pointer, assigning NULL to it and *then* 
deleting it leaks memory.  If you had a "new"'d object, you'd delete it 
and then *maybe* assign NULL to it.

4. It's a C-ism to use NULL for null pointers.  It's better C++ style to 
use 0 instead.

> I may just go with the native MySQL.

Go right on ahead.  The same usage errors with it will give the same 
library errors, since they're just as wrong when using the C API.
MySQL++ Query Loop : Commands Out of Sync?Jordan Hewitt15 Jan
  • Re: MySQL++ Query Loop : Commands Out of Sync?Warren Young15 Jan
    • Re: MySQL++ Query Loop : Commands Out of Sync?Warren Young16 Jan
  • Re: MySQL++ Query Loop : Commands Out of Sync?Martin Gallwey15 Jan
    • Re: MySQL++ Query Loop : Commands Out of Sync?Jordan Hewitt16 Jan