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

MYSQL
MS SQL7.0的数据迁移到MySQL上的一种方法
MySql ODBC进行MYsql和MSsql7的数据转换
MySQL优化全攻略--相关数据库命令
MySQL数据库接口的VC实现与应用(1)
MySQL数据库接口的VC实现与应用(2)
Tomcat5配置MySQL JDBC数据库连接池
如何从MySQL数据库表中检索数据
数据库中MySQL各种字段的取值范围
在Windows上的MySQL UDF开发应用
SCO下用gcc编译调用mysql数据库的程序
获取SQL Server数据库里表占用容量大小
MySQL查询优化技术系列讲座之使用索引
MySQL数据库单一表突破4G限制的实现方法
使用mysqlhotcopy备份MySQL
phpMyAdmin 3.0.1.1 - MySQL管理利器
Linux下MySQL的一些基本使用方法
CentOS编译Mysql 4.0.27数据库问题解决
MySqlODBC进行MYsql和MSsql7的数据转换
MM.MySQL MySQL的JDBC驱动程序的介绍
一个简单的mysql数据库分页的程序模板

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


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