首页 > 数据库 > mysql教程 > Linux下通过C 语言代码来操作MySQL数据库_MySQL

Linux下通过C 语言代码来操作MySQL数据库_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-01 14:02:53
原创
1170 人浏览过

C和C加加

    代码:

    #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安装的路径有关。

 

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
centos7 - git的linux版本没有centos的?
来自于 1970-01-01 08:00:00
0
0
0
学习Linux的先行知识
来自于 1970-01-01 08:00:00
0
0
0
Linux下连接数据库
来自于 1970-01-01 08:00:00
0
0
0
Linux 批量修改文件名
来自于 1970-01-01 08:00:00
0
0
0
Linux中如何查看服务器的日志
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板