Chinaunix首页 | 论坛 | 博客
  • 博客访问: 302282
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 830
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:44
个人简介

一杯茶,一台电脑

文章分类

全部博文(93)

文章存档

2018年(4)

2017年(57)

2016年(32)

分类: Mysql/postgreSQL

2017-12-14 18:08:20


  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstddef>
  4. #include <string>
  5. #include <vector>
  6. #include <WinSock2.h>
  7. #include <mysql.h>
  8. #pragma comment(lib,"libmysql")
  9. using namespace std;

  10. int main(){

  11.     const char *host = "localhost";
  12.     const char *user = "root";
  13.     const char *upwd = "root";
  14.     const char *dbs = "test";
  15.     unsigned int port = 3306;


  16.     MYSQL mydata;

  17.     if(0==mysql_library_init(0,NULL,NULL)){
  18.         cout<<"mysql_library_init() successed!!"<<endl;
  19.     }else{
  20.         cout<<"mysql_library_init() failed!!"<<endl;
  21.         return -1;
  22.     }

  23.     //初始化数据结构
  24.     if(NULL != mysql_init(&mydata)){
  25.         cout<<"初始化数据结构成功!"<<endl;
  26.     }else{
  27.         cout<<"初始化数据结构失败"<<endl;
  28.         return -1;
  29.     }

  30.     //设置数据库编码类型
  31.     if(0==mysql_options(&mydata,MYSQL_SET_CHARSET_NAME,"utf8")){
  32.         cout<<"设置数据库编码类型成功!"<<endl;
  33.     }else{
  34.         cout<<"设置数据库编码类型失败!"<<endl;
  35.         return -1;
  36.     }

  37.     //连接数据库
  38.     if(NULL != mysql_real_connect(&mydata,host,user,upwd,dbs,port,NULL,0)){
  39.         cout<<"数据库连接成功了!"<<endl;
  40.     }else{
  41.         cout<<"数据库连接失败了!!"<<endl;
  42.         return -1;
  43.     }

  44.     //插入数据
  45.     //string sqlstr;
  46.     //sqlstr="INSERT INTO user(`username`, `password`) VALUES ('test', 'test123');";
  47.     //if(0==mysql_query(&mydata,sqlstr.c_str())){
  48.     //    cout<<"插入数据成功"<<endl;
  49.     //}else{
  50.     //    cout<<"插入数据失败了!!"<<endl;
  51.     //}

  52.     string sqlstr_select = " select * from user;";
  53.     //显示查询结果
  54.     MYSQL_RES *result=NULL;
  55.     if(0==mysql_query(&mydata,sqlstr_select.c_str())){
  56.         cout<<"查询成功!"<<endl;

  57.         //一次性取得数据集
  58.         result =mysql_store_result(&mydata);

  59.         //取得并打印行数
  60.         my_ulonglong rowcount = mysql_num_rows(result);
  61.         cout << "row count: " << rowcount << endl;

  62.         //取得并打印各字段的名称
  63.         unsigned int fieldcount = mysql_num_fields(result);
  64.          MYSQL_FIELD *field = NULL;

  65.          for (unsigned int i = 0; i < fieldcount; i++) {
  66.              field = mysql_fetch_field_direct(result, i);
  67.              cout << field->name << "\t\t";
  68.          }
  69.          cout << endl;

  70.          ////打印各行
  71.          //MYSQL_ROW row = NULL;
  72.          //row = mysql_fetch_row(result);
  73.          //while (NULL != row) {
  74.             // for (int i = 0; i < fieldcount; i++) {
  75.             //     cout << row[i] << "\t\t";
  76.             // }
  77.             // cout << endl;
  78.             // row = mysql_fetch_row(result);
  79.          //}


  80.     }else{
  81.         cout << "mysql_query() select data failed" << endl;
  82.         mysql_close(&mydata);
  83.         return -1;
  84.     }

  85.     


  86.     mysql_close(&mydata);
  87.     mysql_library_end();
  88.     return 0;
  89. }

阅读(2125) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~