mysql C API statment and bind 执行查询
#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;
}

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

可以通过以下步骤打开 phpMyAdmin:1. 登录网站控制面板;2. 找到并点击 phpMyAdmin 图标;3. 输入 MySQL 凭据;4. 点击 "登录"。

MySQL是一种开源的关系型数据库管理系统,主要用于快速、可靠地存储和检索数据。其工作原理包括客户端请求、查询解析、执行查询和返回结果。使用示例包括创建表、插入和查询数据,以及高级功能如JOIN操作。常见错误涉及SQL语法、数据类型和权限问题,优化建议包括使用索引、优化查询和分表分区。

MySQL在数据库和编程中的地位非常重要,它是一个开源的关系型数据库管理系统,广泛应用于各种应用场景。1)MySQL提供高效的数据存储、组织和检索功能,支持Web、移动和企业级系统。2)它使用客户端-服务器架构,支持多种存储引擎和索引优化。3)基本用法包括创建表和插入数据,高级用法涉及多表JOIN和复杂查询。4)常见问题如SQL语法错误和性能问题可以通过EXPLAIN命令和慢查询日志调试。5)性能优化方法包括合理使用索引、优化查询和使用缓存,最佳实践包括使用事务和PreparedStatemen

选择MySQL的原因是其性能、可靠性、易用性和社区支持。1.MySQL提供高效的数据存储和检索功能,支持多种数据类型和高级查询操作。2.采用客户端-服务器架构和多种存储引擎,支持事务和查询优化。3.易于使用,支持多种操作系统和编程语言。4.拥有强大的社区支持,提供丰富的资源和解决方案。

Apache 连接数据库需要以下步骤:安装数据库驱动程序。配置 web.xml 文件以创建连接池。创建 JDBC 数据源,指定连接设置。从 Java 代码中使用 JDBC API 访问数据库,包括获取连接、创建语句、绑定参数、执行查询或更新以及处理结果。

在 Docker 中启动 MySQL 的过程包含以下步骤:拉取 MySQL 镜像创建并启动容器,设置根用户密码并映射端口验证连接创建数据库和用户授予对数据库的所有权限

在 CentOS 上安装 MySQL 涉及以下步骤:添加合适的 MySQL yum 源。执行 yum install mysql-server 命令以安装 MySQL 服务器。使用 mysql_secure_installation 命令进行安全设置,例如设置 root 用户密码。根据需要自定义 MySQL 配置文件。调整 MySQL 参数和优化数据库以提升性能。

MySQL在Web应用中的主要作用是存储和管理数据。1.MySQL高效处理用户信息、产品目录和交易记录等数据。2.通过SQL查询,开发者能从数据库提取信息生成动态内容。3.MySQL基于客户端-服务器模型工作,确保查询速度可接受。
