189 8069 5689

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

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

成都创新互联公司专注于烈山网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供烈山营销型网站建设,烈山网站制作、烈山网页设计、烈山网站官网定制、微信小程序开发服务,打造烈山网络公司原创品牌,更为您提供烈山网站排名全网营销落地服务。

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

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

#include "stdafx.h"
#include 
#include 
#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" <name<<":"<

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

#include  
#include  
#include  
#include  
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))<http://jkwzsj.com/article/gohepc.html