在下面的示例中,介绍了使用mysql_stmt_init()、mysql_stmt_prepare()、mysql_stmt_param_count()、mysql_stmt_bind_param()、mysql_stmt_execute()、以及mysql_stmt_affected_rows()创建和填充表的方法。假定mysql变量具有有效的连接句柄。

//tests/mysql_client_test.c

#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);

}

mysql execute stmt_25.2.7.10. mysql_stmt_execute()相关推荐

  1. MySQL优化必须调整的10项配置

    来源:http://www.jb51.net/article/47419.htm 这篇文章主要介绍了MySQL优化必须调整的10项配置,使用这些方法可以让你快速地获得一个稳健的MySQL配置,需要的朋 ...

  2. MySQL 企业监控器 2.3.10 正式版发布

    Oracle于近日发布了 MySQL 企业监控器 2.3.10 正式版. MySQL企业监控器主要用于实施对数据库进行监控和管理.通过它,数据库管理员不但可以获得高级的数据复制和数据库监控功能,同时还 ...

  3. mysql时间加10分钟_将MySQL日期时间格式添加10分钟?

    使用DATE_ADD()将10分钟添加到日期时间格式.以下是语法-select date_add(yourColumnName ,interval 10 minute) from yourTableN ...

  4. mysql execute immediate_使用EXECUTE IMMEDIATE来生成含有绑定变量的SQL

    一个SQL,通过SPM固定它的执行计划,可以通过DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE实现.也可以通地此功能在不修改原SQL的情况下对其加HINT来固定执行计划. ...

  5. MySQL 8.0与MariaDB 10.4,谁更易于填坑补锅?

    文章转载自dbaplus社群公众号 作者介绍 贺春旸,凡普金科DBA团队负责人,<MySQL管理之道:性能调优.高可用与监控>第一.二版作者,曾任职于中国移动飞信.安卓机锋网.致力于Mar ...

  6. mysql execute 存储过程_mysql之存储过程

    存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql 存储过程的优点 #1. 用于替代程序写的SQL语句,实现程序与sql解耦 #2. 可以通 ...

  7. mysql 4.1.10_Mysql4.1.10初级解读

    Mysql4.1.10初级解读 ※※※※※※※※※※※※※※Mysql 初级解读 (所用版本:4.1.10)※※※※※※※※※※※※※※ 安装注意事项 在安装的过程中,请记好您的密码,这是将来登录my ...

  8. MySQL 性能调优的10个方法

    MYSQL 应该是最流行了 WEB 后端数据库.WEB 开发语言最近发展很快,PHP, Ruby, Python, Java 各有特点,虽然 NOSQL 最近越來越多的被提到,但是相信大部分架构师还是 ...

  9. centos安装mysql wsl_如何在 Windows 10 中安装 WSL2 的 Linux 子系统

    什么是WSL? Windows Subsystem for Linux,适用于 Linux 的 Windows 子系统可让开发人员按原样运行 GNU/Linux 环境 - 包括大多数命令行工具.实用工 ...

  10. mysql配置环境变量(win 10)_mysql配置环境变量(win 10)

    1.安装完mysql后就需要配置环境变量 (win 10) 选择"我的电脑",单击右键,选择"属性->高级->环境变量中的系统变量,对 MYSQL_HOME. ...

最新文章

  1. AAAI2020| 超低精度量化BERT,UC伯克利提用二阶信息压缩神经网络
  2. python笔记6--编码
  3. linux c 中 当前函数名 文件名 可变参 不定参 宏使用
  4. Gitlab部署和汉化以及简单运维
  5. Python地信专题 | 基于geopandas的空间数据分析-坐标参考系篇
  6. java主要内存区域_可能是把Java内存区域讲的最清楚的一篇文章
  7. JS-数组和函数冒泡排序递归函数
  8. 如何优雅的在java中统计代码块耗时
  9. 7-7 找最小的字符串 (15 分)
  10. HDU-ACM-2018(母牛的故事)
  11. HTML连续英文字符串强制换行
  12. js异步之setTimeout与setInterval
  13. 【CTF misc python】加密zip和rar文件的枚举解密工具(python代码)
  14. 水滴IP告诉你:IP是什么?动态IP和静态IP有什么区别?
  15. 计算机一级三维饼图,计算机考试excel制作复合饼图的方法
  16. Overleaf使用技巧 (latex公式,latex表格,latex图片排版)
  17. 微信小程序之实现加载动画的旋转方块案例效果(前端学习收藏夹必备)
  18. bzoj 1477 青蛙的约会 拓展欧几里得(详细解析)
  19. Java中数组的写法
  20. 用python判断你是青少年还是老年人

热门文章

  1. python 函数调用自身_Python-函数的递归调用
  2. 2019新版35 U.S.C. 101专利适格性审查指南 新增「抽象概念三分类」及「整合至实际应用」判定标准
  3. WPS以及Office 下 word 文档,使用通配符进行高级替换
  4. URL调用高德地图导航
  5. 雷电模拟器安装frida
  6. maven atuo import
  7. Android 手机锁屏解锁后Activity走了onDestroy
  8. 恒流LED升压驱动芯片2.5V~24V输入【待机功耗低 电流精度高3%】惠海半导体H6911方案分析
  9. 什么是Linux,以及Linux发行版?(update20201118)
  10. 证券期货行业数据模型设计