使用otl,报错:mysql Commands out of sync; you can't run this command now
1、代码如下:
void TestCache(otl_connect& otlConn) { try { char sql[1024] = {0}; sprintf(sql,"call test1(1)"); otl_stream stream(100, sql, otlConn,otl_implicit_select); int id; while(!stream.eof()) { stream>>id; char sql2[1024] = {0}; sprintf(sql2,"call test2(:Id<int>)"); otl_stream stream2(100, sql2, otlConn,otl_implicit_select); stream2<<id;
int ff =0;
while(!stream2.eof()) { stream2>>ff; } } } catch(otl_exception& ex) { printf("ExecuteSql Error, ErrorMsg[%s], Sql[%s]", ex.msg, ex.stm_text); } }
2、执行otl_stream stream2(100, sql2, otlConn,otl_implicit_select);的时候出错,如下:
Commands out of sync; you can't run this command now
特别注意:如果test1 只返回1条或者0条记录,不会导致这个异常。
3、错误原因:mysql上一次的查询没有将结果集释放掉,又进行下一次的查询。
4、otl:在第一个stream读取期间,第二个stream使用了绑定变量,会导致上面的问题,不知道otl内部是怎么封装的。
5、解决办法:
a、第二个stream不使用绑定变量,如下:
void TestCache(otl_connect& otlConn) { try { char sql[1024] = {0}; sprintf(sql,"call test1(1)"); otl_stream stream(100, sql, otlConn,otl_implicit_select); int id; while(!stream.eof()) { stream>>id; char sql2[1024] = {0}; sprintf(sql2,"call test2(%d)",id); otl_stream stream2(100, sql2, otlConn,otl_implicit_select); int ff =0;
while(!stream2.eof()) { stream2>>ff; } } } catch(otl_exception& ex) { printf("ExecuteSql Error, ErrorMsg[%s], Sql[%s]", ex.msg, ex.stm_text); } }
b、先把第一个stream读取完,再进行第二个stream,如下:
void TestCache(otl_connect& otlConn) { try { char sql[1024] = {0}; sprintf(sql,"call test1(1)"); otl_stream stream(100, sql, otlConn,otl_implicit_select); vector<int> intVec; int id; while(!stream.eof()) { stream>>id; intVec.push_back(id); } for(vector<int>::iterator iter = intVec.begin(); iter != intVec.end(); ++iter) { char sql2[1024] = {0}; sprintf(sql2,"call test2(:Id<int>)"); otl_stream stream2(100, sql2, otlConn,otl_implicit_select); stream2<<id; int ff =0;
while(!stream2.eof()) { stream>>ff; } } } catch(otl_exception& ex) { printf("ExecuteSql Error, ErrorMsg[%s], Sql[%s]", ex.msg, ex.stm_text); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人