当前位置:首页 > 信息技术 > 数据库系统 > MySql > VC++6.0连接MySQL(二)

VC++6.0连接MySQL(二)

// 下面的代码是一个实现C++连接MYSQL数据库的很好的例子
// 实现了建表,插入,检索,删表等常用功能
// 在VC++6.0中首先需要做以下几个准备工作
// (1)Tools->Options->Directories->Include files中添加%MySQL_Install_Dir%include
// (2)Tools->Options->Directories->Library files中添加%MySQL_Install_Dir%lib
// (3)Tools->Options->Directories->Library files中添加%MySQL_Install_Dir%libopt(如果有则添加)
// (4)Tools->Options->Directories->Library files中添加%MySQL_Install_Dir%libdebug
// %MySQL_Install_Dir%为你的MySQL实际安装目录, 替换之……

#include
#include
#include  // 注意, winsock.h一定要包含在mysql.h之前
#include
using namespace std;

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libmysql.lib")

// 单步执行, 便于测试, 不想单步执行就注释掉
// #define STEPBYSTEP

void init(); //声明
int main(int argc, char* argv[]) {
init();
try {
  //参数判断
  if(6 != argc) {
   throw -1;
  }
#ifdef STEPBYSTEP
  system("pause");
#endif

  //连接MySQL必备的数据结构
  MYSQL mydata;

  //初始化数据库
  if (0 != mysql_library_init(0, NULL, NULL))
   throw -2;
  cout < < "mysql_library_init() —- SUCCEED!!!" << endl;
#ifdef STEPBYSTEP
  system("pause");
#endif

  //初始化数据结构
  if (NULL == mysql_init(&mydata))
   throw -3;
  cout << "mysql_init() —- SUCCEED!!!" << endl;
#ifdef STEPBYSTEP
  system("pause");
#endif

  //在连接数据库之前,设置额外的连接选项
  //可以设置的选项很多,这里设置字符集,否则无法处理中文
  if (0 != mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk"))
   throw -4;
  cout << "mysql_options() —- SUCCEED!!!" << endl;
#ifdef STEPBYSTEP
  system("pause");
#endif

  //连接数据库
  //在此先附上该函数的原型
  // MYSQL* mysql_real_connect(
  // MYSQL *mysql,
  // const char *host, const char *user, const char *passwd,
  // const char *db, unsigned int port,
  // const char *unix_socket,
  // unsigned long client_flag
  // ) /*资料来源: http://baike.baidu.com/view/8038006.htm*/
  if (NULL == mysql_real_connect(&mydata, argv[1], argv[2],
   argv[3], argv[4], atoi(argv[5]), NULL, 0))
   //这里的地址,用户名,密码,端口可以根据自己本地的情况更改
   throw -5;
  cout << "mysql_real_connect() —- SUCCEED!!!" << endl;
#ifdef STEPBYSTEP
  system("pause");
#endif

  //sql字符串
  string sqlstr;

  //创建一个表
  sqlstr = "CREATE TABLE IF NOT EXISTS user_info";
  sqlstr += "(";
  sqlstr += "user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID',";
  sqlstr += "user_name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User',";
  sqlstr += "user_second_sum INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'";
  sqlstr += ");";
  if (0 != mysql_query(&mydata, sqlstr.c_str())) {
   mysql_close(&mydata);
   throw -6;
  }
  cout << "mysql_query() create table —- SUCCEED!!!" << endl;
#ifdef STEPBYSTEP
  system("pause");
#endif

  //向表中插入数据
  sqlstr = "INSERT INTO user_info(user_name) VALUES('公司名称'),('一级部门'),('二级部门'),('开发小组'),('员工姓名');";
  if (0 != mysql_query(&mydata, sqlstr.c_str())) {
   mysql_close(&mydata);
   throw -7;
  }
  cout << "mysql_query() insert data —- SUCCEED!!!" << endl;
#ifdef STEPBYSTEP
  system("pause");
#endif

  //显示刚才插入的数据
  sqlstr = "SELECT user_id,user_name,user_second_sum FROM user_info;";
  MYSQL_RES *result = NULL;
  if (0 != mysql_query(&mydata, sqlstr.c_str())) {
   mysql_close(&mydata);
   throw -8;
  }
  else {
   cout << "mysql_query() select data —- SUCCEED!!!" << endl;

   //一次性取得数据集
   result = mysql_store_result(&mydata);
   //取得并打印行数
   int rowcount = mysql_num_rows(result);
   cout << "row count: " << rowcount << endl;

   //取得并打印各字段的名称
   unsigned int fieldcount = mysql_num_fields(result);
   MYSQL_FIELD *field = NULL;
   for (unsigned int i = 0; i < fieldcount; i++) {
    field = mysql_fetch_field_direct(result, i);
    cout << field->name < < "tt";
   }
   cout << endl;

   //打印各行
   MYSQL_ROW row = NULL;
   row = mysql_fetch_row(result);
   while (NULL != row) {
    for (int i = 0; i < fieldcount; i++) {
     cout << row[i] << "tt";
    }
    cout << endl;
    row = mysql_fetch_row(result);
   }
  }
#ifdef STEPBYSTEP
  system("pause");
#endif

  //删除刚才建的表
  sqlstr = "DROP TABLE IF EXISTS user_info";
  if (0 != mysql_query(&mydata, sqlstr.c_str())) {
   mysql_close(&mydata);
   throw -9;
  }
  cout << "mysql_query() drop table —- SUCCEED!!!" << endl;

  mysql_free_result(result);
  mysql_close(&mydata);
  mysql_server_end();
  }

  //异常处理
  catch(int ex_i) {
   if(-1 == ex_i) {
    cout << "使用方法: " << argv[0] << " MySQL服务器地址 用户名 密码 数据库 端口号" << endl;
    cout << "例如: " << argv[0] << " localhost user passwd test 3306" << "nn" <<endl;
    system("pause");
    exit(0);
   }
   if(-2 == ex_i)
    cout << "t mysql_library_init() —- FAILED!!!nn" << endl;
   if(-3 == ex_i)
    cout << "t mysql_init() —- FAILED!!!nn" << endl;
   if(-4 == ex_i)
    cout << "t mysql_options() —- FAILED!!!nn" << endl;
   if(-5 == ex_i)
    cout << "t mysql_real_connect() —- FAILED!!!nn" << endl;
   if(-6 == ex_i)
    cout << "t mysql_query() create table —- FAILED!!!nn" << endl;
   if(-7 == ex_i)
    cout << "t mysql_query() insert data —- FAILED!!!nn" << endl;
   if(-8 == ex_i)
    cout << "t mysql_query() select data —- FAILED!!!nn" << endl;
   if(-9 == ex_i)
    cout << "t mysql_query() drop table —- FAILED!!!nn" << endl;
  }

  system("pause");
  return 0;
}

void init() {
cout << "n" << "****************************************" << "n" << endl;
cout << "t Editor: 禹认真" << endl;
cout << "t http://blog.yrz.name" << endl;
cout << "t QQ: 137514797" << endl;
cout << "n" << "****************************************" << "nn" << endl;
}

//如果之前没有包含"%MySQL_Install_Dir%LIB"目录
//链接的时候会提示缺少libmysql.lib, 请从MySQL的安装目录将此文件拷贝到项目工程目录下即可
//如果运行的时候会提示缺少libmysql.dll, 请从MySQL的安装目录将此文件与最终生成的目标文件放在一起
//或者将此文件拷贝到操作系统的system32目录下即可

 

 

VC++6.0连接MySQL(二):等您坐沙发呢!

发表评论

表情
还能输入210个字