|
3#
楼主 |
发表于 2009-4-23 15:53:41
|
只看该作者
那个手册我有一只在看呢
我也有一个demo,如下(是windows下的)
#include <windows.h>
#include <process.h>
#include <iostream>
#include <string>
#include "mysql.h" //安装的mysql的头文件所在的位置
using namespace std;
string host="localhost";
string user="root";
string pwd="904";
string dbname="jjdb";
string sql="select * from test";
unsigned int port=3306;
int status;
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,sql.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] <<"|"<< row[2] << endl;
}
mysql_free_result(result);
mysql_close(mysql);
}
编译通过,运行没问题
我修改了一下程序,对表test ,insert了以行新的数据,如下
#include <windows.h>
#include <process.h>
#include <iostream>
#include <string>
#include "mysql.h" //安装的mysql的头文件所在的位置
using namespace std;
string host="localhost";
string user="root";
string pwd="904";
string dbname="jjdb";
string sql="select * from test";
unsigned int port=3306;
int status;
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,sql.c_str());
if(status !=0 ){
cout << "query failure!" << endl;
}
cout << "the status is :" << status << endl;
result=mysql_store_result(mysql);
MYSQL_ROW_OFFSET off=mysql_row_tell(result);
while(row=mysql_fetch_row(result)){
cout <<row[0]<<"|"<< row[1] <<"|"<< row[2] << endl;
}
cout<<endl;
sql="insert into test values('aaa','f',20)";
if(mysql_query(mysql,sql.c_str()))
{
cout << "query failure!" << endl;
}
result=mysql_store_result(mysql);
mysql_row_seek(result,off);
while(row=mysql_fetch_row(result)){
cout <<row[0]<<"|"<< row[1] <<"|"<< row[2] << endl;
}
mysql_free_result(result);
mysql_close(mysql);
}
编译通过,运行时到了第二个while循环要输出新获取的结果集的时候就报错,把while那段代码注释掉就没问题。
我想解决的问题是
1.在同一次数据库的连接当中,能不能多次获得结果集吗?
2.我上面的程序是什么问题,不能访问新获取的结果集吗?
3.如何对结果集进行修改并把更新反馈到表中,还是只能通过sql语句直接对表进行更新操作? |
|