当前位置: 首页 > 图文教程 > 数据库 > MYSQL > Linux下通过C++语言代码来操作MySQL数据库

MYSQL
SQL Server与Oracle数据库在查询优化上的差异
轻松掌握怎样从Windows命令行启动MySQL
教你轻松掌握MaxDB和MySQL之间的协同性
教你轻松了解MySQL数据库中的结果字符串
解析:轻松了解 MySQL中损坏的MyISAM表
解析:MySQL 数据库搜索中大小写敏感性
实例解析:MySQL 实例管理器识别的命令
快速掌握 Mysql数据库对文件操作的封装
帮助你分析MySQL的数据类型以及建库策略
从MySQL导大量数据的程序实现方法
MySQL数据库中设列的默认值为Now()的介绍
如何将txt文本中的数据轻松导入MySQL表中
带你深入了解MySQL数据库系统参数的优化
初学MySql5 所应了解的知识和常见问题
MYSQL数据库实用学习资料之常用命令集合
MySQL数据库配置技巧
Mysql数据库常用命令
计划备份mysql数据库
一次MySQL性能优化实战
MySQL乱码问题深层分析

MYSQL 中的 Linux下通过C++语言代码来操作MySQL数据库


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 35 ::
收藏到网摘: n/a

代码:

#include

#include

#include "/usr/local/include/mysql/mysql.h" //安装的mysql的头文件所在的位置

using namespace std;

string host="数据库地址";

string user="用户名";


string pwd="密码";

string dbname="数据库";

string sql="查询语句";

unsigned int port=3309;#端口号

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[1] <<"|"<< row[2] << endl;

}

mysql_free_result(result);

mysql_close(mysql);

}


编译:


g++ -o test test.cpp -lmysqlclient -I/usr/local/include/mysql/ -L/usr/local/lib/mysql


后面的那些用来指定mysql安装的时候包含路径和库文件路径,具体与你机器上的mysql安装的路径有关。