Mysql_num_rows()Segfaults [英] Mysql_num_rows() Segfaults

查看:210
本文介绍了Mysql_num_rows()Segfaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++和MySQL C API(版本5.1.31 ubuntu2)编写程序。但是,如果查询是 UPDATE ,那么当执行行RowsReturned = mysql_num_rows(Result);时会出现分段错误错误。

  //此代码片段仅包含相关代码
MYSQL_RES * Result;
long RowsReturned;

MYSQL_RES * MYSQLDB :: RunQuery(const char * Query)
{
if(mysql_query(Connection,Query))
{
std :: cout < Error:< mysql_error(connection);
exit(1);
}

Result = mysql_store_result(Connection);
RowsReturned = mysql_num_rows(Result);

return Result;
}



使用g ++编译4.3.3(g ++ test.cpp -I / usr / include / mysql -L / usr / lib / mysql -lmysqlclient_r -o Test)



提前感谢。

  //此代码片段包含整个类代码
class MYSQLDB
{

public:
void Connect(const char * DB);
MYSQL_RES * RunQuery(const char * Query);
long NumRows();
void Close();
MYSQL_ROW GetRow();
private:
MYSQL * Connection;
MYSQL_RES * Result;
MYSQL_ROW Row;

long RowsReturned;
};

void MYSQLDB :: Connect(const char * DB)
{
Connection = mysql_init(NULL);

if(Connection == NULL)
{
std :: cout< Error:< mysql_error(Connection);
exit(1);
}

if(mysql_real_connect(Connection,localhost,root,password,DB,0,NULL,0)== NULL)
{
std :: cout<< Error:< mysql_error(Connection);
exit(1);
}
}

MYSQL_RES * MYSQLDB :: RunQuery(const char * Query)
{
if(mysql_query(Connection,Query))
{
std :: cout<< Error:< mysql_error(Connection);
exit(1);
}

Result = mysql_store_result(Connection);
RowsReturned =(long)mysql_num_rows(Result); //错误!!!!!!!

return Result;
}

long MYSQLDB :: NumRows()
{
return RowsReturned;
}

void MYSQLDB :: Close()
{
mysql_free_result(Result);
mysql_close(Connection);
}

MYSQL_ROW MYSQLDB :: GetRow()
{
return mysql_fetch_row(Result)
}


解决方案

/ p>

如果语句没有返回结果集(例如,它是一个INSERT语句),mysql_store_result()会返回一个空指针。



您正在更新,因此您的结果为NULL。



尝试这样:


$ b b

  Result = mysql_store_result(Connection); 
if(Result){
RowsReturned = mysql_num_rows(Result);
} else {
RowsReturned = 0;
}


I'm writing a program using C++ and the MySQL C API (version 5.1.31 ubuntu2). However, if the query is UPDATE then I get a Segmentation Fault error when executing the line "RowsReturned = mysql_num_rows( Result );".

//this code snippet contains only the relevant code
MYSQL_RES *Result;
long RowsReturned;

MYSQL_RES *MYSQLDB::RunQuery( const char* Query )
{
    if( mysql_query( Connection, Query) )
    {
        std::cout << "Error: " << mysql_error( Connection );
        exit(1);
    }

    Result = mysql_store_result( Connection );
    RowsReturned = mysql_num_rows( Result );

    return Result;
}

Compiled using g++ 4.3.3 (g++ test.cpp -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient_r -o Test)

Thanks in advance.

//this snippet contains the entire class code
class MYSQLDB
{

public:
    void Connect( const char* DB );
    MYSQL_RES *RunQuery( const char* Query );  
    long NumRows();
    void Close();
    MYSQL_ROW GetRow();
private:
    MYSQL *Connection;
    MYSQL_RES *Result;
    MYSQL_ROW Row;

    long RowsReturned;
};

void MYSQLDB::Connect( const char* DB )
{
    Connection = mysql_init( NULL );

    if( Connection == NULL )
    {
        std::cout << "Error: " << mysql_error( Connection );
        exit( 1 );
    }

    if ( mysql_real_connect( Connection, "localhost", "root", "password", DB, 0, NULL, 0 ) == NULL)
    {
        std::cout << "Error: " << mysql_error( Connection );
        exit(1);
    }
}

MYSQL_RES *MYSQLDB::RunQuery( const char* Query )
{
    if( mysql_query( Connection, Query) )
    {
        std::cout << "Error: " << mysql_error( Connection );
        exit(1);
    }

    Result = mysql_store_result( Connection );
    RowsReturned = (long)mysql_num_rows( Result ); //ERROR!!!!!!!

    return Result;
}

long MYSQLDB::NumRows()
{
    return RowsReturned;
}

void MYSQLDB::Close()
{
    mysql_free_result( Result );
    mysql_close( Connection );
}

MYSQL_ROW MYSQLDB::GetRow()
{
    return mysql_fetch_row( Result );
}

解决方案

From the mysql documentation:

mysql_store_result() returns a null pointer if the statement didn't return a result set (for example, if it was an INSERT statement).

You are updating so you have a NULL as results.

Try something like this:

Result = mysql_store_result( Connection );
if (Result) {
    RowsReturned = mysql_num_rows( Result );
} else {
    RowsReturned = 0;
}

这篇关于Mysql_num_rows()Segfaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆