mysql execute stmt_mysql_stmt_execute()
mysql_stmt_execute()
int
mysql_stmt_execute(MYSQL_STMT *stmt)
描述
mysql_stmt_execute()执行与语句句柄相关的预处理查询。在该调用期间,将当前绑定的参数标记符的值发送到服务器,服务器用新提供的数据替换标记符。
如果语句是UPDATE、DELETE或INSERT,通过调用mysql_stmt_affected_rows(),可发现更改、删除或插入的总行数。如果这是诸如SELECT等能生成结果集的语句,调用任何其他能导致查询处理的函数之前,必须调用mysql_stmt_fetch()来获取数据。关于如何获取结果的更多信息,请参见25.2.7.11节,“mysql_stmt_fetch()”。
对于生成结果集的语句,执行语句之前,可通过调用mysql_stmt_attr_set(),请求mysql_stmt_execute()为语句打开光标。如果多次执行某一语句,在打开新的光标前,mysql_stmt_execute()将关闭任何已打开的光标。
返回值
如果执行成功,返回0。如果出现错误,返回非0值。
错误
·CR_COMMANDS_OUT_OF_SYNC
以不恰当的顺序执行了命令。
·CR_OUT_OF_MEMORY
内存溢出。
·CR_SERVER_GONE_ERROR
MySQL服务器不可用。
·CR_SERVER_LOST
在查询过程中,与服务器的连接丢失。
·CR_UNKNOWN_ERROR
出现未知错误。
示例:
在下面的示例中,介绍了使用mysql_stmt_init()、mysql_stmt_prepare()、mysql_stmt_param_count()、mysql_stmt_bind_param()、mysql_stmt_execute()、以及mysql_stmt_affected_rows()创建和填充表的方法。假定mysql变量具有有效的连接句柄。
#define STRING_SIZE 50
#define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table"
#define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 INT,\
col2 VARCHAR(40),\
col3 SMALLINT,\
col4 TIMESTAMP)"
#define INSERT_SAMPLE "INSERT INTO test_table(col1,col2,col3) VALUES(?,?,?)"
MYSQL_STMT *stmt;
MYSQL_BIND bind[3];
my_ulonglong affected_rows;
int param_count;
short small_data;
int int_data;
char str_data[STRING_SIZE];
unsigned long str_length;
my_bool is_null;
if (mysql_query(mysql, DROP_SAMPLE_TABLE))
{
fprintf(stderr, " DROP TABLE failed\n");
fprintf(stderr, " %s\n", mysql_error(mysql));
exit(0);
}
if (mysql_query(mysql, CREATE_SAMPLE_TABLE))
{
fprintf(stderr, " CREATE TABLE failed\n");
fprintf(stderr, " %s\n", mysql_error(mysql));
exit(0);
}
/* Prepare an INSERT query with 3 parameters */
/* (the TIMESTAMP column is not named; the server */
/* sets it to the current date and time) */
stmt = mysql_stmt_init(mysql);
if (!stmt)
{
fprintf(stderr, " mysql_stmt_init(), out of memory\n");
exit(0);
}
if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE)))
{
fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
fprintf(stdout, " prepare, INSERT successful\n");
/* Get the parameter count from the statement */
param_count= mysql_stmt_param_count(stmt);
fprintf(stdout, " total parameters in INSERT: %d\n", param_count);
if (param_count != 3) /* validate parameter count */
{
fprintf(stderr, " invalid parameter count returned by MySQL\n");
exit(0);
}
/* Bind the data for all 3 parameters */
memset(bind, 0, sizeof(bind));
/* INTEGER PARAM */
/* This is a number type, so there is no need to specify buffer_length */
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].buffer= (char *)&int_data;
bind[0].is_null= 0;
bind[0].length= 0;
/* STRING PARAM */
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].buffer= (char *)str_data;
bind[1].buffer_length= STRING_SIZE;
bind[1].is_null= 0;
bind[1].length= &str_length;
/* SMALLINT PARAM */
bind[2].buffer_type= MYSQL_TYPE_SHORT;
bind[2].buffer= (char *)&small_data;
bind[2].is_null= &is_null;
bind[2].length= 0;
/* Bind the buffers */
if (mysql_stmt_bind_param(stmt, bind))
{
fprintf(stderr, " mysql_stmt_bind_param() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Specify the data values for the first row */
int_data= 10; /* integer */
strncpy(str_data, "MySQL", STRING_SIZE); /* string */
str_length= strlen(str_data);
/* INSERT SMALLINT data as NULL */
is_null= 1;
/* Execute the INSERT statement - 1*/
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Get the total number of affected rows */
affected_rows= mysql_stmt_affected_rows(stmt);
fprintf(stdout, " total affected rows(insert 1): %lu\n",
(unsigned long) affected_rows);
if (affected_rows != 1) /* validate affected rows */
{
fprintf(stderr, " invalid affected rows by MySQL\n");
exit(0);
}
/* Specify data values for second row, then re-execute the statement */
int_data= 1000;
strncpy(str_data, "The most popular Open Source database", STRING_SIZE);
str_length= strlen(str_data);
small_data= 1000; /* smallint */
is_null= 0; /* reset */
/* Execute the INSERT statement - 2*/
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, " mysql_stmt_execute, 2 failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Get the total rows affected */
affected_rows= mysql_stmt_affected_rows(stmt);
fprintf(stdout, " total affected rows(insert 2): %lu\n",
(unsigned long) affected_rows);
if (affected_rows != 1) /* validate affected rows */
{
fprintf(stderr, " invalid affected rows by MySQL\n");
exit(0);
}
/* Close the statement */
if (mysql_stmt_close(stmt))
{
fprintf(stderr, " failed while closing the statement\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
注释:关于使用预处理语句函数的完整示例,请参见文件tests/mysql_client_test.c。该文件可从MySQL源码分发版获得,或从BitKeeper源码仓库获得。
mysql execute stmt_mysql_stmt_execute()相关推荐
- mysql execute using_MySQL:将过程参数传递给EXECUTE USING语句
这是 MySQL 5.1. (注意:我意识到有更好的方法来做这个特定的例子,这不是我真正的代码) 这是我想要做的: 下面的过程被创建,但是当我调用它时,我得到"ERROR 1210(HY00 ...
- mysql execute 存储过程_mysql之存储过程
存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql 存储过程的优点 #1. 用于替代程序写的SQL语句,实现程序与sql解耦 #2. 可以通 ...
- php mysql execute语法_PHP PDOStatement::execute讲解
PDOStatement::execute PDOStatement::execute - 执行一条预处理语句(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0) 说明 ...
- mysql execute指令_MYSQL简单命令
常用 mysql 操作指令: 连接:mysql -u用户名 -p密码 退出:mysql>exit; 建数据库:mysql>create database ???; 显示数据库:mysql& ...
- mysql execute immediate_使用EXECUTE IMMEDIATE来生成含有绑定变量的SQL
一个SQL,通过SPM固定它的执行计划,可以通过DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE实现.也可以通地此功能在不修改原SQL的情况下对其加HINT来固定执行计划. ...
- mysql execute immediate_PostgreSQL中function中实现类似Oracle的execute immediate的功能
PostgreSQL中function中实现类似Oracle的execute immediate和dbms_output_putline的功能 首先需要说明的是,PostgreSQL中没有像Oracl ...
- mysql点击execute_MySQL execute、executeUpdate、executeQuery三者的区别
execute.executeUpdate.executeQuery三者的区别(及返回值) 一.boolean execute(String sql) 允许执行查询语句.更新语句.DDL语句. 返回值 ...
- MySQL 5.7 PREPARE、EXECUTE、DEALLOCATE语句介绍
PREPARE语句准备好一条SQL语句,并分配给这条SQL语句一个名字供之后调用.准备好的SQL语句通过EXECUTE命令执行,通过DEALLOCATE PREPARE命令释放掉. 语句的名字不区分大 ...
- MySQL 预处理语句prepare、execute、deallocate的使用,执行动态sql
MySQL官方将prepare.execute.deallocate统称为PREPARE STATEMENT. 我习惯称其为[预处理语句]. 其用法十分简单, PREPARE stmt_name FR ...
- mysql deallocate,MySQL中prepare与execute以及deallocate预处理语句的使用教程
这篇文章主要介绍了MySQL中预处理语句prepare.execute与deallocate的使用教程,需要的朋友可以参考下 MySQL官方将prepare.execute.deallocate统称为 ...
最新文章
- 4 关卡编辑器_虚幻引擎4与生存游戏产生化学反应,超真实开放世界手游诞生
- android 之四大组件的Activity详解
- 机器人学习--感知环境数据集
- 小知识点BeanUtils.copyProperties
- P3899 [湖南集训]谈笑风生
- redis源码剖析(十二)—— RDB持久化
- 【计算机网络复习 物理层】2.1.3 码元、波特、速率、带宽
- 2020年上半年巨量引擎手机行业白皮书
- linux RPM包安装、更新、删除等操作命令简明总结, 如何查看yum安装的软件路径 ?...
- WIFI6 5G信道、频宽对应关系
- iastora怎么改成ahci_WIN7系统IDE改成AHCI就蓝屏怎么办_win7硬盘模式ide改成ahci蓝屏处理方法-win7之家...
- hzhost防asp攻击函数
- win7系统计算机无最小化,win7我的电脑图标没了win7我的电脑不见解决方法(图)
- 笔记本外接显示器,过一段会自动休眠
- 网上教学系统mysql课程设计_基于学生信息管理系统的数据库系统课程设计(MySQL)...
- java计算机毕业设计Web企业差旅在线管理系统(附源码、数据库)
- 淮南师范学院计算机是几本,淮南师范学院是几本 是一本还是二本
- 模拟股票交易系统 3 (柜台系统)
- linux CPU主频设置
- 获取最新的中国IP的脚本,给ROS可以使用的脚本