c/c++中如何 取得执行存储过程的多结果集...

yeerh 2011-01-22 03:41:59
--创建表
CREATE TABLE userinfo
(
userid int,
username varchar(31)
);
CREATE TABLE areainfo
(
areaid int,
areaname varchar(31)
);
--插入数据
INSERT INTO test.areainfo
(areaid, areaname)
VALUES (1, '上海');

INSERT INTO test.areainfo
(areaid, areaname)
VALUES (2, '成都');

INSERT INTO test.areainfo
(areaid, areaname)
VALUES (3, '北京');

--------------------------------------
INSERT INTO test.userinfo
(userid, username)
VALUES (1, '张三');

INSERT INTO test.userinfo
(userid, username)
VALUES (2, '李四');

INSERT INTO test.userinfo
(userid, username)
VALUES (3, '王王');

----创建存储过程
create procedure getUserArea2
(keyid int)
begin
select * from test.areainfo;
select * from test.userinfo;
end

---------------------------------
--问题..
在c/C++ 中如何调用这个存储过程并获取结果...

代码:

#include <windows.h>
#include <iostream>
#include <string>
#include "mysql.h" //在工程设置中,包含在mysql的sdk中的include文件夹下

using namespace std;
string host="localhost"; //登陆mysql使用的主机名
string user="root"; //登陆用户

string pwd="sa"; //填写自己数据库密码
string dbname="test"; //您要登陆的数据库名
string sqlProcess="call getUserArea(3)"; //sql存储过程语句
unsigned int port=3306; //数据库端口,默认为3306,也可以填写你自己制定的端口
int status; //数据库执行返回的状态,sql语句执行成功返回0

int main()
{
MYSQL *mysql;
mysql=mysql_init(0); //接口,初始化
MYSQL_RES *result;
MYSQL_ROW row; //返回数据行


//连接数据库
if( mysql_real_connect( mysql, host.c_str(), user.c_str(), pwd.c_str(), dbname.c_str(), port, NULL, CLIENT_FOUND_ROWS ) == NULL )
{
cout << "connect failure!" << endl;
return EXIT_FAILURE;
}
else
{
cout << "connect success!" << endl;
}
mysql_set_character_set( mysql, "gbk" );


//执行存储过程调用
status = mysql_query( mysql, sqlProcess.c_str( ) );
if( status != 0 )
{
cout << "query failure!" << endl;
}
cout << "the status is :" << status << endl;

result = mysql_store_result( mysql );
//显示结果 ....此外出错..
while( row = mysql_fetch_row( result ) )
{
cout << row[0] <<"|" << row[1] << endl;
}
}
...全文
377 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeerh 2011-01-22
  • 打赏
  • 举报
回复
谢谢..根据mysql_next_result()
在MySql5.1的文档中找到解决方法
详见:
http://dev.mysql.com/doc/refman/5.1/zh/apis.html#c-api-multiple-queries
的25.2.9. 多查询执行的C API处理
除了使用mysql_next_result(), 在连接时还添加参数 CLIENT_MULTI_STATEMENTS
新代码如下.


#include <windows.h>
#include <iostream>
#include <string>
#include "mysql.h" //在工程设置中,包含在mysql的sdk中的include文件夹下

using namespace std;
string host = "localhost"; //登陆mysql使用的主机名
string user = "root"; //登陆用户

int status; //数据库执行返回的状态,sql语句执行成功返回0
string pwd = "sa"; //填写自己数据库密码
string dbname = "test"; //您要登陆的数据库名
unsigned int port = 3306; //数据库端口,默认为3306,也可以填写你自己制定的端口
string sql = "select * from userinfo"; //sql语句,下面使用接口在c语言中执行该语句
string sqlProcess = "call getUserArea2(3)"; //sql存储过程语句

int main()
{
MYSQL *mysql;
mysql = mysql_init(0); //接口,初始化
MYSQL_RES *result;
MYSQL_ROW row; //返回数据行

//连接数据库
if (mysql_real_connect(mysql, host.c_str(), user.c_str(), pwd.c_str(), dbname.c_str(), port, NULL, CLIENT_FOUND_ROWS | CLIENT_MULTI_STATEMENTS) == NULL)
{
cout << "connect failure!" << endl;
return EXIT_FAILURE;
}
else
{
cout << "connect success!" << endl;
}
mysql_set_character_set(mysql, "gbk");


//执行存储过程调用
status = mysql_query(mysql, sqlProcess.c_str());
if (status != 0)
{
cout << "query failure!" << endl;
}
cout << "the status is :" << status << endl;

do
{
if (result = mysql_store_result(mysql))
{
//显示结果
while (row = mysql_fetch_row(result))
{
cout << row[0] << "|" << row[1] << endl;
}
//process_result_set(result);
mysql_free_result(result);
}
}
while (!mysql_next_result(mysql));
}
小小小小周 2011-01-22
  • 打赏
  • 举报
回复

顶楼上2位大牛。周末也不休息
iihero_ 2011-01-22
  • 打赏
  • 举报
回复
这里有一个例子:
http://iihero.com/?id=12

里边有一个子函数:
test_more_results()
ACMAIN_CHM 2011-01-22
  • 打赏
  • 举报
回复
mysql_next_result()

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧