当前位置:主页 > c/c++教程 > C++访问mysql

C++连接mysql的方法(直接调用C-API)

发布:2022-06-21 07:23:51 59


给寻找编程代码教程的朋友们精选了C++相关的编程文章,网友耿海露根据主题投稿了本篇教程内容,涉及到C++、mysql、C++访问mysql相关内容,已被361网友关注,相关难点技巧可以阅读下方的电子资料。

C++访问mysql

我装的是5.1版本,需要的头文件有

把需要的文件添加进去,然后再把 libmysql.lib放到项目目录里,文件在mysql安装目录 lib 下面.

#include "stdafx.h"
#include <iostream>
#include <winsock2.h>
#include "mysql.h"
//#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib,"libmysql.lib")
using namespace std;
int main(int argc, char* argv[])
{
 
  mysql_library_init(NULL,0,0); 
  MYSQL mysql;
  mysql_init(&mysql);
 
  if(0==mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"utf8"))//设置字符集
  {
    cout << "设置字符集成功\n\n" <<endl;
  }
 
  if(!mysql_real_connect(&mysql,"localhost","root","kwgkwg","test",0,NULL,CLIENT_MULTI_STATEMENTS))//连接数据库
  {
    cout << "not connect mysql" << endl;
  }else
  {
    cout << "welcome to mysql\n\n\n";
  }
 
  mysql_query(&mysql,"select * from demo1");     //执行SQL语句
  MYSQL_RES *result=mysql_store_result(&mysql);    //获取资源
  int rowcount=mysql_num_rows(result);        //获取记录数
  unsigned int fieldcount=mysql_num_fields(result);  //获取字段数
 
  //cout << rowcount << endl;
 
  MYSQL_FIELD *field=NULL;              //字段
  MYSQL_ROW row=NULL;             //记录
  while(row=mysql_fetch_row(result))
  {
    for(unsigned int i=0;i<fieldcount;i++)
    {
      field=mysql_fetch_field_direct(result,i);
      cout<<field->name<<":"<<row[i] <<"\n";
    }
  } 
 
 
  mysql_free_result(result);
  mysql_close(&mysql);
  mysql_server_end();
  mysql_library_end();
   
  return 0;
}

C++访问 (直接调用C-API)

#include <iostream> 
#include <windows.h> 
#include <mysql.h> 
#include <string> 
static const char host[32] = "localhost"; 
static const char user[32] = "test"; 
static const char passwd[32] = "passwd"; 
static const char db[32] = "test"; 
/** 
mysql> select * from t; 
+----+ 
| id | 
+----+ 
| 1 | 
+----+ 
1 row in set (0.00 sec) 
mysql> delimiter // 
mysql> create procedure get_t(in t1 int) 
  -> begin 
  -> select id from t where id=t1; 
  -> end 
  -> // 
Query OK, 0 rows affected (0.05 sec) 
mysql> call get_t(1); 
  -> // 
+----+ 
| id | 
+----+ 
| 1 | 
+----+ 
1 row in set (0.00 sec) 
Query OK, 0 rows affected (0.00 sec) 
*/ 
void test_more_results(MYSQL* h) 
{ 
  char str[512] = "insert into test_num values(101);insert into test_num values(122);commit;"; 
  int r = mysql_real_query(h, str, strlen(str)); 
  if (r) 
  { 
    const char * error = mysql_error(h); 
    std::cout<<"*** Connection Error " << error << std::endl; 
  } 
  do 
  { 
    MYSQL_RES* res = mysql_store_result(h); 
    mysql_free_result(res); 
  } 
  while ( (0 == mysql_next_result(h)) ); 
   
} 
void test_proc_stmt(MYSQL* h) 
{ 
  MYSQL* mysql_ = h; 
  MYSQL_BIND     bind; 
  MYSQL_BIND     obind[1]; 
  // test_more_results(mysql_); 
  MYSQL_STMT *hStmt = mysql_stmt_init(mysql_); 
  my_bool true_value= 1; 
  mysql_stmt_attr_set(hStmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &true_value);   
  char sql[] = "call get_t(?)"; 
  //char sql[] = "select id from t where id=?"; 
  if (mysql_stmt_prepare(hStmt, sql, strlen(sql))) 
  { 
    std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; 
    mysql_stmt_reset(hStmt); 
    if (mysql_stmt_prepare(hStmt, sql, strlen(sql))) 
    { 
      std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; 
      mysql_close(mysql_); 
      exit( -1); 
    } 
  } 
  int id = 1; 
  unsigned long id_len = 0; 
  memset(&bind, 0, sizeof(bind)); 
  bind.buffer_type = FIELD_TYPE_LONG; 
  bind.buffer = (void*)&id; 
  bind.is_unsigned = true; 
  bind.length = &id_len; 
  // bind[0].buffer_length = sizeof(id); 
  // bind[0].is_null = 0; 
   
  if (mysql_stmt_bind_param(hStmt,(MYSQL_BIND*)(&bind)) != 0) 
  { 
    std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; 
    mysql_close(mysql_); 
    exit( -1); 
  } 
  if (mysql_stmt_execute(hStmt) != 0) 
  { 
    std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; 
    mysql_close(mysql_); 
    exit( -1); 
  } 
  int t2; 
  memset(obind, 0, sizeof(obind)); 
  obind[0].buffer_type= MYSQL_TYPE_LONG; 
  obind[0].buffer= (char *)&t2; 
  obind[0].buffer_length = sizeof(t2); 
   
  if (mysql_stmt_bind_result(hStmt, (MYSQL_BIND*)&obind[0]) != 0) 
  { 
    std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; 
    mysql_close(mysql_); 
    exit( -1); 
  } 
  if ( mysql_stmt_store_result(hStmt) != 0 ) 
  { 
    std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; 
    mysql_close(mysql_); 
    exit( -1); 
  } 
   
   
  int rows = mysql_stmt_num_rows(hStmt); 
  for (int i=0; i<rows; i++) 
  { 
    if (mysql_stmt_fetch(hStmt) == 0) 
    { 
      std::cout<<"id = "<<t2<<std::endl; 
    } 
  } 
  mysql_stmt_free_result(hStmt); 
  mysql_stmt_close(hStmt); 
} 
// 
// Just for demo only. 
// 
int main() 
{ 
  MYSQL*       mysql_ = NULL; 
  MYSQL_RES*     result_ = NULL; 
  MYSQL_ROW      row_; 
  mysql_ = mysql_init(mysql_); 
  // if (mysql_real_connect(mysql_, host, user, passwd, db, 3306, NULL, CLIENT_MULTI_STATEMENTS) == NULL) 
  if (mysql_real_connect(mysql_, host, user, passwd, db, 3306, NULL, CLIENT_MULTI_STATEMENTS) == NULL) 
  { 
    const char * error = mysql_error(mysql_); 
    std::cout<<"*** Connection Error " << error << std::endl; 
    return -1; 
  } 
  mysql_autocommit(mysql_, false); 
  std::string encodeStr = "set names 'gbk'"; 
  mysql_real_query(mysql_, encodeStr.c_str(), encodeStr.size()); 
   
  /* 
  const char* tmpTableName = "t"; // assume you are querying the table 't' 
  char str[512]; 
  int cnt = 0; 
  sprintf(str,"select count(*) as cnt from %s", tmpTableName); 
  mysql_real_query(mysql_, str, strlen(str)); 
  result_ = mysql_store_result(mysql_); 
  while (row_ = mysql_fetch_row(result_)) 
  { 
    // get the field value 
    if (row_[0]) 
    { 
      std::cout<<"count = "<<row_[0]<<std::endl; 
      // convert it into int 
      cnt = atoi(row_[0]); 
      std::cout<<"cnt value = "<<row_[0]<<std::endl; 
    } 
  } 
  mysql_free_result(result_); 
  test_more_results(); 
  */ 
  test_proc_stmt(mysql_); 
   
  do 
  { 
    MYSQL_RES* res = mysql_store_result(mysql_); 
    mysql_free_result(res); 
  } 
  while ( (0 == mysql_next_result(mysql_)) ); 
   
  test_proc_stmt(mysql_); 
  mysql_close(mysql_); 
  return 0; 
} 

这样就差不多了,大家可以根据需要选择。


相关文章

  • C++实现数字转换为十六进制字符串的方法

    发布:2022-04-16

    这篇文章主要介绍了C++实现数字转换为十六进制字符串的方法,涉及C++操作数字与字符串转换的相关技巧,需要的朋友可以参考下


  • C/C++指针和取地址的方法

    发布:2022-04-01

    C/C++指针和取地址的方法,需要的朋友可以参考一下


  • 老生常谈C++中实参形参的传递问题

    发布:2022-04-15

    下面小编就为大家带来一篇老生常谈C++中实参形参的传递问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧


  • C++树之遍历二叉树实例详解

    发布:2022-04-11

    这篇文章主要给大家介绍了关于C++树之遍历二叉树的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧


  • C++实现归并排序的实例

    发布:2021-10-08

    这篇文章主要为大家详细介绍了C++实现归并排序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下


  • C++中strstr函数的实现方法总结

    发布:2022-06-18

    给网友朋友们带来一篇关于C++的教程,这篇文章主要介绍了C++中strstr函数的实现方法总结的相关资料,希望通过本文能帮助到大家,让大家掌握这部分内容,需要的朋友可以参考下


  • 浅谈C++函数声明后面加throw()的作用(必看)

    发布:2022-04-14

    下面小编就为大家带来一篇浅谈C++函数声明后面加throw()的作用(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧


  • C++类与对象的详细说明

    发布:2022-04-16

    这篇文章主要为大家详细介绍了C++的类与对象,使用数据库,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助


网友讨论