代码
#include <cstdlib>
#include <string>
#include /usr/local/include/mysql/mysqlh //安装的mysql的头文件所在的位置
using namespace std;
string host=数据库地址;
string user=用户名;
string pwd=密码;
string dbname=数据库;
string sql=查询语句;
unsigned int port=;#端口号
int status;
int main(){
MYSQL *mysql;
mysql=mysql_init();
MYSQL_RES *result;
MYSQL_ROW row;
if(mysql_real_connect(mysqlhostc_str()userc_str()pwdc_str()dbnamec_str()portNULLCLIENT_FOUND_ROWS)==NULL){
cout << connect failure! << endl;
return EXIT_FAILURE;
}else{
cout << connect success! << endl;
}
mysql_set_character_set(mysqlgbk);
status=mysql_query(mysqlsqlc_str());
if(status != ){
cout << query failure! << endl;
}
cout << the status is : << status << endl;
result=mysql_store_result(mysql);
while(row=mysql_fetch_row(result)){
cout << row[] <<|<< row[] << endl;
}
mysql_free_result(result);
mysql_close(mysql);
}
编译
g++ o test testcpp lmysqlclient I/usr/local/include/mysql/ L/usr/local/lib/mysql
后面的那些用来指定mysql安装的时候包含路径和库文件路径具体与你机器上的mysql安装的路径有关