C ++ MySQL ++删除查询语句脑部杀手问题

我对C ++中的MySQL ++连接器比较陌生,并且已经有了一个非常烦人的问题! 我设法让存储过程正常工作,但是我遇到了删除语句的问题。我看起来高低,没有找到带有示例的文档。 首先我想可能代码需要在调用存储过程后释放查询/连接结果,但当然MySQL ++没有free_result方法......或者是吗? 无论如何,这就是我所拥有的:
#include <iostream>
#include <stdio.h>
#include <queue>
#include <deque>
#include <sys/stat.h>
#include <mysql++/mysql++.h>
#include <boost/thread/thread.hpp>
#include "RepositoryQueue.h"

using namespace boost;
using namespace mysqlpp;

class RepositoryChecker
{
private:
    bool _isRunning;
Connection _con;
public:
RepositoryChecker()
{
    try
    {
        this->_con = Connection(false);
        this->_con.set_option(new MultiStatementsOption(true));
        this->_con.set_option(new ReconnectOption(true));
        this->_con.connect("**", "***", "***", "***");

        this->ChangeRunningState(true);
    }
    catch(const Exception& e)
    {
        this->ChangeRunningState(false);
    }
}
/**
* Thread method which runs and creates the repositories
*/
void CheckRepositoryQueues()
{
    //while(this->IsRunning())
    //{
        std::queue<RepositoryQueue> queues = this->GetQueue();

        if(queues.size() > 0)
        {
            while(!queues.empty())
            {
                RepositoryQueue &q = queues.front();
                char cmd[256];
                sprintf(cmd, "svnadmin create /home/svn/%s/%s/%s", q.GetPublicStatus().c_str(),
                    q.GetUsername().c_str(), q.GetRepositoryName().c_str());
                    if(this->DeleteQueuedRepository(q.GetQueueId()))
                    {
                        printf("query deleted?n");
                    }
                    printf("Repository created!n");
                queues.pop();
            }
        }
        boost::this_thread::sleep(boost::posix_time::milliseconds(500));
    //}
}
protected:
/**
* Gets the latest queue of repositories from the database
* and returns them inside a cool queue defined with the 
* RepositoryQueue class.
*/
std::queue<RepositoryQueue> GetQueue()
{
    std::queue<RepositoryQueue> queues;

    Query query = this->_con.query("CALL sp_GetRepositoryQueue();");
    StoreQueryResult result = query.store();
    RepositoryQueue rQ;

    if(result.num_rows() > 0)
    {
        for(unsigned int i = 0;i < result.num_rows(); ++i)
        {
            rQ = RepositoryQueue((unsigned int)result[i][0],
                                (unsigned int)result[i][1],
                                (String)result[i][2],
                                (String)result[i][3],
                                (String)result[i][4],
                                (bool)result[i][5]);
            queues.push(rQ);
        }
    }

    return queues;
}
/**
* Allows the thread to be shut off.
*/
void ChangeRunningState(bool isRunning)
{
    this->_isRunning = isRunning;
}
/**
* Returns the running value of the active thread.
*/
bool IsRunning()
{
    return this->_isRunning;
}
/**
* Deletes the repository from the mysql queue table. This is
* only called once it has been created.
*/
bool DeleteQueuedRepository(unsigned int id)
{
    char cmd[256];
    sprintf(cmd, "DELETE FROM RepositoryQueue WHERE Id = %d LIMIT 1;", id);
    Query query = this->_con.query(cmd);
    return (query.exec());
}
};
我已经删除了所有其他方法,因为它们不需要...... 基本上它是不起作用的DeleteQueuedRepository方法,GetQueue工作正常。 PS:这是在Linux OS(Ubuntu服务器)上 非常感谢, 肖恩     
已邀请:
  MySQL ++没有free_result方法......或者是吗? 它不需要一个。当结果对象在
GetQueue()
结束时超出范围时,将自动释放与其关联的所有内存。
this->_con = Connection(false);
这里有三个问题: 创建
RepositoryChecker
对象时,您已经创建了一个
Connection
对象。如果你需要将不同的参数传递给它的构造函数,你可以在
RepositoryChecker
构造函数的初始化列表中执行,而不是在它的正文中。阅读您的C ++书籍。 你在这里所做的是a)创建一个默认的
Connection
对象,然后b)创建一个不同的
Connection
对象,关闭异常,然后c)用第二个覆盖第一个。如果有效,那就非常低效。 MySQL ++
Connection
对象过去曾经遇到过复制问题,所以如果你使用的是旧版本的库,它可以解释你的问题。 你告诉
Connection
对象(它创建的每个对象,甚至间接地,这意味着MySQL ++中的几乎所有东西)你不希望它抛出异常,但是你将它包装在一个大的
try
块中。选一个。 我建议使用例外&mdash; MySQL ++中的默认值&mdash;鉴于您的代码目前的结构方式。如果在
DeleteQueuedRepository()
中出现查询错误,则无法查看发生了什么,因为您只是将
false
传递给调用者,由于调用中没有
else
子句,因此被忽略。如果这样做,请在
catch
块中记录
e.what()
消息。你现在就把这些信息扔掉了。 在某些地方,您使用的结构看起来更像是Python(或者可能是JavaScript),而不是C ++。这让我想知道你的问题是不是由于其他一些误用C ++而造成的损害。 特别是在这一行,你明确地使用了
this
指针,在C ++中没有必要。这段代码完全相同:
 _con = Connection(false);
尽管如此,应该使用
RepositoryChecker
ctor初始化列表来完全替换该行。 继续...
sprintf(cmd, "DELETE FROM RepositoryQueue WHERE Id = %d LIMIT 1;", id);
正如其他人评论的那样,你最好使用
Query
流接口:
Query q = _con.query();
q << "DELETE FROM RepositoryQueue WHERE Id = " << id << " LIMIT 1";
这有几个好处: 修复了建议将
%d
更改为
%u
的人所暗示的类型安全问题。 C ++ IOStreams会为您解决这个问题。 如果需要,自动引用插入流中的数据。 (在这种情况下,它不是。) 防止任何运行缓冲区结束的可能性。你可以在这里使用不可移植的
snprintf()
,但为什么呢? 如果你对
printf()
真的很满意,那就是模板查询界面。
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
您是否阅读了用户手册中的线程章节?你没有在MySQL ++中免费获得线程安全。您的问题可能是由于内存损坏。 Warren Young,MySQL ++ Maintainer     
尝试在sprintf中将“%d”更改为“%u”。     

要回复问题请先登录注册