首頁 資料庫 mysql教程 mysql C API statment and bind 执行查询

mysql C API statment and bind 执行查询

Jun 07, 2016 pm 03:40 PM
and api bind mysql 執行

#include string #include mysql/mysql.h #include default.h typedefstd::stringstring; structmysql_parm{ stringhost; stringuser; stringpassword; stringdatabase; stringunixsock; }; classDBSTMT; classDBMysql; classDBSTMT{ DBSTMT(const DBSTMT);

#include
#include
#include

typedef std::string string;

struct mysql_parm{
 string host;
 string user;
 string password;
 string database;
 string unixsock;
};
class DBSTMT;
class DBMysql;

class DBSTMT{
 DBSTMT(const DBSTMT&);
 DBSTMT& operator=(const DBSTMT&);
 MYSQL_STMT* stmt_;
public:
 DBSTMT(pcsz_t query,DBMysql& mysql);
 void execute(){
  if(mysql_stmt_execute(stmt_))
   throw mysql_stmt_error(stmt_);
 }
 
 void execute(MYSQL_BIND* bind){
  if(mysql_stmt_execute(stmt_))
   throw mysql_stmt_error(stmt_);
  if(mysql_stmt_bind_result(stmt_,bind)){
   throw mysql_stmt_error(stmt_);
  }
  if(mysql_stmt_store_result(stmt_))
   throw mysql_stmt_error(stmt_);
 }

 //void execute(){
 // if(mysql_stmt_execute(stmt_))
 //  throw mysql_stmt_error(stmt_);
 //}
 
 void bind(MYSQL_BIND* bind){
  if(mysql_stmt_bind_param(stmt_,bind) )
   throw mysql_stmt_error(stmt_);
 }
 
 int fetch(){
  return mysql_stmt_fetch(stmt_)==0;
 }
 ~DBSTMT(){
  if(stmt_){
   mysql_stmt_close(stmt_);
  }
 }
 
};


class DBMysql{
 DBMysql(const DBMysql&);
 DBMysql&operator=(const DBMysql&);
MYSQL * mysqlPtr_;

uint32_t errno_;
protected:
 friend class DBSTMT;
 MYSQL_STMT* _createSTMT(){
  MYSQL_STMT *ret=mysql_stmt_init(mysqlPtr_);
  if(ret)
   return ret;
  errno_=mysql_errno(mysqlPtr_);
  throw mysql_error(mysqlPtr_);
 }
public:
 const char* strerr(){
  return mysql_error(mysqlPtr_);
 }
 DBMysql():mysqlPtr_(NULL){
  mysqlPtr_=mysql_init(NULL);
  if(NULL== mysqlPtr_)
   throw "Mysql :outof memory";
 }
 void open(const mysql_parm& parm){
  if(!mysql_real_connect(mysqlPtr_,
   parm.host.c_str(),
   parm.user.c_str(),
   parm.password.c_str(),
   parm.database.c_str(),
   0,
   parm.unixsock.c_str(),
   0 ))
  {
   errno_=mysql_errno(mysqlPtr_);
   throw(mysql_error(mysqlPtr_));
  }
 }

 void close(){
  if(mysqlPtr_)
  {
   mysql_close(mysqlPtr_);
   mysqlPtr_=NULL;
  }
 }
  
};


DBSTMT::DBSTMT(pcsz_t query,DBMysql& mysql):stmt_(NULL){
 stmt_=mysql._createSTMT();
 if(!stmt_)
  throw mysql.strerr();
 if( mysql_stmt_prepare(stmt_,query,strlen(query)) )
 {
  //const char* err=
  
  throw mysql_stmt_error(stmt_);
 }
 
}


struct account{
 char user[36];
 byte password[16];
 uint32_t status;
 uint32_t id;
};

#define DECL_BIND(h,n)/
class bind_##h:public h{/
 typedef h parent;/
 MYSQL_BIND  _bind[n];/
 my_bool   _is_null[n];/
 unsigned long _length[n];/
public:/
 bind_##h(){/
  int i=0;/
  bzero(_bind,sizeof(_bind));


#define BIND_BIN(x,l)/
 _bind[i].buffer_type= MYSQL_TYPE_STRING;/
 _bind[i].buffer= (char *)&(parent::x);/
 _bind[i].buffer_length= l;/
 _bind[i].is_null= _is_null+i;/
 _bind[i].length= _length+i;/
 ++i;

#define BIND_INT(x)/
 _bind[i].buffer_type= MYSQL_TYPE_LONG;/
 _bind[i].buffer= (char *)&(parent::x);/
 _bind[i].buffer_length= 0;/
 _bind[i].is_null= _is_null+i;/
 _bind[i].length= _length+i;/
 ++i;

#define END_BIND(h)  }/
 operator MYSQL_BIND*(){/
 return _bind;/
 }/
};


//account acc;
//
//DECL_BIND(4)
// BIND_BIN(acc.user,32);
// BIND_BIN(acc.password,16);
// BIND_INT(acc.status);
// BIND_INT(acc.id);

// smt.executeAndStore(bind);
//END_BIND(4)

DECL_BIND(account,4)
 BIND_BIN(user,32)
 BIND_BIN(password,16)
 BIND_INT(status)
 BIND_INT(id)
END_BIND(account)

int main(){

 try{
  
  DBMysql mysql;
  mysql_parm parm;
  parm.host="localhost";
  parm.user="root";
  parm.password="mypwd";
  parm.unixsock="/var/lib/mysql/mysql.sock";
  parm.database="testdb";
  mysql.open(parm);
  
  DBSTMT smt("select user,password,status,id from account",mysql);
  DBSTMT smt1("insert into account(user,password,status) value(?,?,?)",mysql);
 //
  
  bind_account acc;
  
  smt.execute(acc);
  
  while(smt.fetch()){
   //acc.user[length[0]]=0;
   //acc.user,
   printf("%s %d %d/n",acc.user,acc.status,acc.id);
   //printf("%d %d/n",acc.status,acc.id);
   
   ;
   
  };
  smt1.bind(acc);
  smt1.execute();
  
  
  
 }catch(const char* err){
  printf("error:%s/n",err);
 }
 
 return 0;
 
}


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

MySQL的角色:Web應用程序中的數據庫 MySQL的角色:Web應用程序中的數據庫 Apr 17, 2025 am 12:23 AM

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

laravel入門實例 laravel入門實例 Apr 18, 2025 pm 12:45 PM

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

docker怎麼啟動mysql docker怎麼啟動mysql Apr 15, 2025 pm 12:09 PM

在 Docker 中啟動 MySQL 的過程包含以下步驟:拉取 MySQL 鏡像創建並啟動容器,設置根用戶密碼並映射端口驗證連接創建數據庫和用戶授予對數據庫的所有權限

解決數據庫連接問題:使用minii/db庫的實際案例 解決數據庫連接問題:使用minii/db庫的實際案例 Apr 18, 2025 am 07:09 AM

在開發一個小型應用時,我遇到了一個棘手的問題:需要快速集成一個輕量級的數據庫操作庫。嘗試了多個庫後,我發現它們要么功能過多,要么兼容性不佳。最終,我找到了minii/db,這是一個基於Yii2的簡化版本,完美地解決了我的問題。

centos7如何安裝mysql centos7如何安裝mysql Apr 14, 2025 pm 08:30 PM

優雅安裝 MySQL 的關鍵在於添加 MySQL 官方倉庫。具體步驟如下:下載 MySQL 官方 GPG 密鑰,防止釣魚攻擊。添加 MySQL 倉庫文件:rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm更新 yum 倉庫緩存:yum update安裝 MySQL:yum install mysql-server啟動 MySQL 服務:systemctl start mysqld設置開機自啟動

centos安裝mysql centos安裝mysql Apr 14, 2025 pm 08:09 PM

在 CentOS 上安裝 MySQL 涉及以下步驟:添加合適的 MySQL yum 源。執行 yum install mysql-server 命令以安裝 MySQL 服務器。使用 mysql_secure_installation 命令進行安全設置,例如設置 root 用戶密碼。根據需要自定義 MySQL 配置文件。調整 MySQL 參數和優化數據庫以提升性能。

laravel框架安裝方法 laravel框架安裝方法 Apr 18, 2025 pm 12:54 PM

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

MySQL和PhpMyAdmin:核心功能和功能 MySQL和PhpMyAdmin:核心功能和功能 Apr 22, 2025 am 12:12 AM

MySQL和phpMyAdmin是強大的數據庫管理工具。 1)MySQL用於創建數據庫和表、執行DML和SQL查詢。 2)phpMyAdmin提供直觀界面進行數據庫管理、表結構管理、數據操作和用戶權限管理。

See all articles