2

我使用 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: ) 有什么问题?

4

1 回答 1

0

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

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

您的参数的列名是vOutvOut2。但在后一行:

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

您将它们称为vOut1vOut2。没有名为 name 的列vOut1,因为您命名了它vOut,因此出现无效的列索引错误。

于 2012-04-22T14:21:41.813 回答