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

MYSQL
MySql:存储程序、函数、触发程序及复制:常见问题
MySQL:数据库系统体系结构之概念模式
MySQL:最小函数依赖集知多少
MySQL:小编浅谈关系代数中的语义
如何运用SQL Server 2005中的模板参数
实例:NStarfaiNet的SQL XML文件示范
SQL Server 2000桌面引擎默认配置空口令漏洞
MySQL 5.0.16 乱码问题处理办法
索引对查询条件顺序的影响
从SQL备份文件中导入现存数据库
利用C#实现分布式数据库查询
用ORACLE8i修复数据库坏块的三种方法
Sql Server2000+Sql Server Mobile Edition配置合并复制
MySQL与ASP.NET配合更强大
防范SQL注入攻击的新办法
如何修改遗失的MySQL的ROOT用户密码
在win2003下MySQL数据库每天自动备份
如何提高MySQL性能
实例讲解MySQL数据库的查询优化技术
了解MYSQL数据库调度与锁定的问题

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 79 ::
收藏到网摘: 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安装的路径有关。