mySQL Connector / C问题

时间:2017-06-17 20:30:42

标签: mysql c mysql-connector-c

ALL,

    std::wstring query2 = L"SELECT cols.column_name, cols.data_type, cols.character_maximum_length, cols.character_octet_length, cols.numeric_precision, cols.numeric_scale, cols.column_default, cols.is_nullable, cols.ordinal_position, (CASE WHEN kcu.column_name = cols.column_name THEN 1 ELSE 0 END) as pk_flag FROM information_schema.columns cols, information_schema.key_column_usage kcu WHERE kcu.constraint_name = 'PRIMARY' AND kcu.table_schema = cols.table_schema AND kcu.table_name = cols.table_name AND cols.table_schema = ? AND cols.table_name = ?;";
    MYSQL_BIND params[2];
    unsigned long str_length1, str_length2;
    str_data1 = new char[strlen( schema_name )], str_data2 = new char[strlen( table_name )];
    memset( params, 0, sizeof( params ) );
    str_length1 = strlen( schema_name );
    str_length2 = strlen( table_name );
    strncpy( str_data1, schema_name, strlen( schema_name ) );
    strncpy( str_data2, table_name, strlen( table_name ) );
    params[0].buffer_type = MYSQL_TYPE_STRING;
    params[0].buffer = (char *) str_data1;
    params[0].buffer_length = strlen( schema_name );
    params[0].is_null = 0;
    params[0].length = &str_length1;
    params[1].buffer_type = MYSQL_TYPE_STRING;
    params[1].buffer = (char *) str_data2;
    params[1].buffer_length = strlen( table_name );
    params[1].is_null = 0;
    params[1].length = &str_length2;
    res2 = mysql_stmt_init( m_db );
    if( !res2 )
    {
        std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_error( m_db ) );
        errorMsg.push_back( err );
        return 1;
    }
    if( mysql_stmt_prepare( res2, m_pimpl->m_myconv.to_bytes( query2.c_str() ).c_str(), query2.length() ) )
    {
        std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_error( m_db ) );
        errorMsg.push_back( err );
        return 1;
    }
    if( mysql_stmt_bind_param( res2, params ) )
    {
        std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_error( m_db ) );
        errorMsg.push_back( err );
        return 1;
    }
    if( mysql_stmt_execute( res2 ) )
    {
        std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_error( m_db ) );
        errorMsg.push_back( err );
        return 1;
    }
    MYSQL_ROW table_def;
    MYSQL_RES *queryResult1 = mysql_store_result( m_db );
    if( queryResult1 )
    {
        while( ( table_def = mysql_fetch_row( queryResult1 ) ) )
        {
            std::wstring fieldName = m_pimpl->m_myconv.from_bytes( table_def[0] );
            std::wstring fieldType = m_pimpl->m_myconv.from_bytes( table_def[1] );
        }
    }
    if( mysql_stmt_close( res2 ) )
    {
        std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_error( m_db ) );
        errorMsg.push_back( err );
        return 1;
    }
    delete str_data1;
    str_data1 = NULL;
    delete str_data2;
    str_data2 = NULL;

所有mysql_()函数都返回成功,但是,queryResult指针为NULL。此外,我可以在Workbench中成功执行此查询 - 它返回一些行。

我在Gentoo Linux上对mySQL-5.6.35使用最新的Connector / C。

我可能错过了一些非常愚蠢的东西,但我不知道是什么。有人可以提供第二双眼睛吗?

谢谢。

2 个答案:

答案 0 :(得分:0)

显然,当您使用预准备语句时,您无法使用mysql_fetch_row()。您应该使用mysql_stmt_bind_result()/mysql_stmt_fetch()对。

答案 1 :(得分:0)

在调试模式下重新编译Connector / C并尝试调试器后,问题就解决了。