问题描述
我正在为MariaDB Connector C进行包装。在典型情况下,开发人员不知道存储在字段中的数据长度。如我所知,获取字段实际长度的一种方法是将长度缓冲区传递给 MysqL_stmt_bind_result ,然后通过调用 MysqL_stmt_fetch_column 来获取每一列。但是我不明白 MysqL_stmt_fetch_column 函数的工作方式,因为我遇到了内存损坏和应用程序中止的情况。
这就是我要达到的目标的方式
// preparations here
...
if (!MysqL_stmt_execute(stmt))
{
int columnNum = MysqL_stmt_field_count(stmt);
if (columnNum > 0)
{
MysqL_RES* Metadata = MysqL_stmt_result_Metadata(stmt);
MysqL_FIELD* fields = MysqL_fetch_fields(Metadata);
MysqL_BIND* result = new MysqL_BIND[columnNum];
std::memset(result,sizeof (MysqL_BIND) * columnNum);
std::vector<unsigned long> lengths;
lengths.resize(columnNum);
for (int i = 0; i < columnNum; ++i)
result[i].length = &lengths[i];
if (!MysqL_stmt_bind_result(stmt,result))
{
while (true)
{
int status = MysqL_stmt_fetch(stmt);
if (status == 1)
{
m_lastError = MysqL_stmt_error(stmt);
isOK = false;
break;
}
else if (status == MysqL_NO_DATA)
{
isOK = true;
break;
}
for (int i = 0; i < columnNum; ++i)
{
my_bool isNull = true;
if (lengths.at(i) > 0)
{
result[i].buffer_type = fields[i].type;
result[i].is_null = &isNull;
result[i].buffer = malloc(lengths.at(i));
result[i].buffer_length = lengths.at(i);
MysqL_stmt_fetch_column(stmt,result,i,0);
if (!isNull)
{
// here I'm trying to read a result and I'm getting a valid result only from the first column
}
}
}
}
}
}
如果我将数组放入 MysqL_stmt_fetch_column ,那么我将获取唯一有效的第一个字段,所有其他字段都是垃圾。如果将单个MysqL_BIND结构放入此函数,那么我将在第74个字段上放弃应用程序(有趣的是,它始终是该字段)。如果我使用另一个MysqL_BIND数组,则情况与第一种情况相同。
请帮助我了解如何正确使用它!谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)