mysql - 在 C 应用程序中调用 MySQL 存储过程出现错误

标签 mysql c

我想在用 C 编写的应用程序中使用 MySQL 的 CURSOR

我创建了一个存储过程

CREATE PROCEDURE curdemo(IN id INT)
BEGIN
    DECLARE a INT;
    DECLARE flag BOOL DEFAULT FALSE;
    DECLARE done INT DEFAULT FALSE;
    DECLARE cur1 CURSOR FOR SELECT test.Cars.Id FROM test.Cars;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur1;

    read_loop: LOOP
            FETCH cur1 INTO a;
            IF done THEN
                    LEAVE read_loop;
            END IF;

            IF flag THEN
                    SELECT test.Cars.Price FROM test.Cars WHERE test.Cars.id = a;
                    LEAVE read_loop;
            END IF;

            IF a = id THEN
                    SET flag = TRUE;
            END IF;
    END LOOP;

    CLOSE cur1;
    END//

这是我在数据库测试中名为 Cars 的表

+------+------------+--------+

| Id   | Name       | Price  |

+------+------------+--------+

|    1 | Audi       |  52642 |

|    2 | Mercedes   |  57127 |

|    3 | Skoda      |   9000 |

|    4 | Volvo      |  29000 |

|    5 | Bentley    | 350000 |

|    6 | Citroen    |  21000 |

|    7 | Hummer     |  41400 |

|    8 | Volkswagen |  21600 |

+------+------------+--------+

当我从 C 应用程序使用以下查询时,我得到的结果 = NULL,因此仅当 id = 8 时才会出现段错误,否则它工作正常

 snprintf(statement, 100, "CALL curdemo('%d')", id);
    // Call stored procedure query
    if ( mysql_query(device_table_conn, statement) )
    {
            error_function(device_table_conn);
            //return 1;
    }

    result = mysql_use_result(device_table_conn);

mysql_use_result() 如果发生错误,则返回 NULL

但如果 CALL 返回 NULL 或空结果,则不应返回 NULL。

最佳答案

终于明白了。

按照处理多个语句的步骤进行操作: https://dev.mysql.com/doc/refman/5.0/en/c-api-multiple-queries.html

这是代码

    snprintf(statement, 100, "CALL curdemo('%d')", id);
    if ( mysql_query(device_table_conn, statement) )
    {
            error_function(device_table_conn);
            //return 1;
    }

    do{
            // did current statement return data?
            result = mysql_store_result(device_table_conn);
            if (result)
            {
                    // yes; process rows and free the result set
                    num_fields = mysql_num_fields(result);

                    while ((row = mysql_fetch_row(result)))
                    {
                            for ( i = 0; i < num_fields; i++ )
                            {
                                    printf("%s\n", row[i]);
                            }
                    }

                    mysql_free_result(result);
            }
            else
            {
                    mysql_free_result(result);
            }
            // more results? -1 = no, >0 = error, 0 = yes (keep looping)
            if ((status = mysql_next_result(device_table_conn)) > 0)
            {
                    ;
            }
    } while (status == 0);

    mysql_close(device_table_conn);

这里,对于任何错误的 id(表中不存在)以及 id = 8 时,它不会返回任何内容。

关于mysql - 在 C 应用程序中调用 MySQL 存储过程出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32142234/

相关文章:

mysql 从工作 "prepare"语句创建 View

c# - 搭建 MYSQL 数据库时出现错误 : The method or operation is not implemented.

c - 使用 scanf 连续读取字符串和 int 时遇到问题

c - 为什么 stdlib.h 充满了外部函数原型(prototype)和 gcc 对此的差异

c - 在平衡搜索树中插入元素

mysql - 引用具有特定字段的输出表

mysql - MariaDB 配置更改不生效

javascript - 自动刷新 Google map 标记

C 程序在给字符串赋值后崩溃

c - SDL函数名称?