ubuntu C++下测试mysql的代码
#include <iostream>
#include <mysql/mysql.h>
using namespace std;
int test_mysql(){
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
//initialize MYSQL structure
mysql_init(&mysql);
//connect to database
mysql_real_connect(&mysql,"localhost","root","2699","talk",3306,0,0);
//execute query
mysql_query(&mysql,"select id,name from test");
//get result set
result=mysql_store_result(&mysql);
//process result set
while((row=mysql_fetch_row(result))){
fprintf(stdout,"%s - %s\n",row[0],row[1]);
}
//clean up
mysql_free_result(result);
mysql_close(&mysql);
}
#include <mysql/mysql.h>
using namespace std;
int test_mysql(){
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
//initialize MYSQL structure
mysql_init(&mysql);
//connect to database
mysql_real_connect(&mysql,"localhost","root","2699","talk",3306,0,0);
//execute query
mysql_query(&mysql,"select id,name from test");
//get result set
result=mysql_store_result(&mysql);
//process result set
while((row=mysql_fetch_row(result))){
fprintf(stdout,"%s - %s\n",row[0],row[1]);
}
//clean up
mysql_free_result(result);
mysql_close(&mysql);
}