CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    609

    flush mysql++ select queue?

    Is there a quick way to flush the select queue (if any) without causing a crash?

    I tried doing this:

    Code:
    while(m_lastqueryresult.fetch_row());
    But the issue there is if the last query was not a select query, or if there was no query at all, this will crash.

    The issue is, if I try to run a query following a select query, if I did not select every single row, then try to select one last row, it will give a sync error.

    So rather then have to call fetch_row each time to prevent a crash I just want to build it in my sql connector class so that it flushes it every time I do a query so that query wont crash.
    http://www.uovalor.com :: Free UO Server

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: flush mysql++ select queue?

    I am not very conversant with mysql++, but if there isn't a routine in the resultset class (or whatever is returned as part of the object return on call to execution of the query), then you will have to call fetch_row until there are no more rows available.

    Also, fetch_row should be called after a check to see if there are rows available or not. So, to that order the while loop doesn't look robust.

    I did not understand what you meant by:
    Quote Originally Posted by Red Squirrel
    But the issue there is if the last query was not a select query, or if there was no query at all, this will crash.

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  4. #4
    Join Date
    Jul 2007
    Posts
    609

    Re: flush mysql++ select queue?

    Quote Originally Posted by exterminator View Post

    I did not understand what you meant by:
    But the issue there is if the last query was not a select query, or if there was no query at all, this will crash.
    Say I call fetch_row on a mysql resource but the last query was not actually a select query, it crashes. So I can't just add a while(fetch_row) in my query command.


    This is how a typical query is done: (assuming we're already connected)

    Code:
    UseQueryResult MySQLConnector::DoQuery(string querystr,bool & goterror)
    {
    	UseQueryResult res;
    	
    	if(CON==NULL)
    	{
    		goterror=true;
    		Debug(1,"Unable to perform query: " + querystr + " - Not connected to db!");
    		return res;
    	}	
    	
    	Query thequery = CON->query(querystr);	
    	
    	res = thequery.use();	
    	
    	Debug(4,"Performing SQL query: " + querystr);
    	
    	if(CON->errnum())
    	{
    		goterror=true;
    		string err=CON->error();
    		m_error=err;
    		Debug(1,"ERROR: " + err);
    	}
    	else m_MySqlAffectedRows=thequery.affected_rows();
    	
    	thequery.reset();
    	
    	return res;
    }
    That's part of my wraper class. So to use it I usually go like this:

    Code:
    	bool goterror=false;
    	
    	//query vhosts
    	UseQueryResult res = Core->DoSqlQuery("SELECT * FROM vhosts ORDER BY vhuserid, vhdomain WHERE vhuserid='" + GetOwnerUser() + "';",goterror);
    	
    	if(goterror)
    	{
    		Core->Debug(1,"Error while retrieving virtual host information");
    		return false;
    	}
    	
    	SubTitle("Del",25,0,0);
    	SubTitle("Hostname",200,0,0);
    	SubTitle("Http Root",0,0,0);
    	SubTitle(" ",25,0,0);
    	
    	
    	while(Row row = res.fetch_row())
    	{
    		CheckBox("delete_" + string(row["vhid"]),0); //show delete checkbox
    		TextL(string(row["vhdomain"]),0,0,0);//show domain
    		TextL(string(row["vhdirectory"]),0,0,0);
    		Link("apache","Edit",PvUtil::P(1,(uli)row["vhid"]));
    	}
    	
    	
    	SubmitButton("delete","Delete Selected");


    In that particular example there's no chance of it crashing as I have the fetch_row in a while loop.

    But in a situation where I only want 1 result but there's a chance that there are less or more, is where it becomes a big problem.

    My attempted solution was to just add a while() that calls fetchrow in the query function but that did not work.

    So my "fix" is to just call it in a loop after an actual select query, but that's kinda unclean. There must be an easier way.
    http://www.uovalor.com :: Free UO Server

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: flush mysql++ select queue?

    So, you don't have a problem as of now. Right?
    Quote Originally Posted by Red Squirrel View Post
    But in a situation where I only want 1 result but there's a chance that there are less or more, is where it becomes a big problem.

    My attempted solution was to just add a while() that calls fetchrow in the query function but that did not work.
    I did not understand. Are you saying that your code fails if there are no rows returned? I don't see how that should happen when fetch_row's return (Row object) would evaluate to false in that case (overloaded bool operator). Just like the regular cases where you have data and you keep doing fetch_row and one past the end, the expression evaluates to false and exists the loop.

    Or are you saying you get more than 1 row and you just wanted 1 row? May be your query is incorrect. Or may be, you could do fetch_row for the first row, use the data and then do fetch_row in an empty while loop till all results are consumed by the client code (your code) if the rest of the returned rows are not useful to you. But still the primary problem here would be the query.

    One suggestion though. If you are not dealing with huge result sets being returned, why don't you use the Query::storein() or Query::store() members to pull back the results at once from the server?

    Also, I think I am not sure, but if you are using .use() member function even on calls to queries that don't return rows, you should change that to the other calls like Query::execute()/Query::exec().

    If you running multiple queries and want to process results for all of them - here is an example from their user manual - http://tangentsoft.net/mysql++/doc/h....html#connopts

  6. #6
    Join Date
    Jul 2007
    Posts
    609

    Re: flush mysql++ select queue?

    The only issue I've run into is having to call fetch row twice when I only want/get one result. The 2nd fetch row returns false and seems to "flush" the queue, but I was hoping there was a function made specifically for that, so I can just call it before each query, but suppose I can live with the way I'm doing it now.

    I also prefer not using store as when dealing with database data normally the amount of data increases as the application is used, (more records, etc).
    http://www.uovalor.com :: Free UO Server

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured