c ++ mysql存储过程多输出参数错误

时间:2012-04-22 11:30:47

标签: c++ mysql

我使用的是Freebsd 8.0 amd64,mysql-client-5.1.61,mysql-connector-c ++ - 1.1.0,mysql-server-5.1.61和gcc 4.2.1这是有用的吗

stmt->execute("CREATE PROCEDURE p(OUT retval VARCHAR(50)) BEGIN SELECT \"Ulf joined MySQL more than 5 years ago\" INTO retval; END;");

 pstmt.reset(con->prepareStatement("CALL p(@output)"));
 pstmt->execute();

 pstmt.reset(con->prepareStatement("SELECT @output AS _answer"));
 res.reset(pstmt->executeQuery());
 while (res->next())
     cout << "\t... _answer: " << res->getString("_answer") << endl;

而这不是

stmt->execute("CREATE PROCEDURE p(OUT retval VARCHAR(50),OUT retval1 VARCHAR(50)) BEGIN SELECT \"Ulf joined MySQL more than 5 years ago\" INTO retval;SELECT \"Ulf joined MySQL more than 5 years ago\" INTO retval1;  END;");
pstmt.reset(con->prepareStatement("CALL p(@out1,@out2)"));
pstmt->execute();

pstmt.reset(con->prepareStatement("SELECT @out1 AS vOut,@out2 as vOut2"));
res.reset(pstmt->executeQuery());
while (res->next())
   cout << res->getString("vOut1") << res->getString("vOut2") << endl;

并返回ERR: MySQLPreparedResultSet::getString: invalid 'columnIndex' (MySQL error code: 0, SQLState: ) 有什么问题?

1 个答案:

答案 0 :(得分:0)

假设你逐字复制了你的代码,我认为问题是由一个简单的拼写错误引起的。在这一行:

pstmt.reset(con->prepareStatement("SELECT @out1 AS vOut,@out2 as vOut2"));

参数的列名称为vOutvOut2。但在后一行:

cout << res->getString("vOut1") << res->getString("vOut2") << endl;

您可以将其称为vOut1vOut2。没有名称为vOut1的列,因为您将其命名为vOut,因此无效的列索引错误。