随笔 - 576  文章 - 0  评论 - 62  阅读 - 219万

使用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); } }
复制代码

 

 

posted on   Andy Niu  阅读(8208)  评论(0编辑  收藏  举报
编辑推荐:
· 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训练数据并当服务器共享给他人
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示