本文主要向大家介绍了MySQL数据库之MySQL Prepared Statement ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

Summary: in this tutorial, you will learn how to use MySQL prepared statement to make your queries execute faster and more secure.

Introduction to MySQL Prepared Statement

Prior MySQL version 4.1, the query is sent to the MySQL server in the textual format. In turn, MySQL returns the data to the client using textual protocol. MySQL has to parse the query fully and coverts the result set into a string before returning it to the client.

The textual protocol has serious performance implication. To resolve this problem, MySQL added a new feature called prepared statement since version 4.1.

The prepared statement takes advantage of client/server binary protocol. It passes query that contains placeholders (?) to the MySQL server as the following example:

SELECT *  FROM products  WHERE productCode = ?

When MySQL executes this query with different productcode values, it does not have to parse the query fully. As a result, this helps MySQL execute the query faster, especially when MySQL executes the query multiple times. Because the prepared statement uses placeholders (?), this helps avoid many variants of SQL injection hence make your application more secure.

MySQL prepared statement usage

In order to use MySQL prepared statement, you need to use other three MySQL statements as follows:

PREPARE – Prepares statement for execution.

EXECUTE – Executes a prepared statement preparing by a PREPARE statement.

DEALLOCATE PREPARE – Releases a prepared statement.

The following diagram illustrates how to use the prepared statement:

MySQL prepared statement example

Let’s take a look at an example of using the MySQL prepared statement.

PREPARE stmt1 FROM 'SELECT productCode, productName                     FROM products                     WHERE productCode = ?'; SET @pc = 'S10_1678'; EXECUTE stmt1 USING @pc; DEALLOCATE PREPARE stmt1;

First we used the PREPARE statement to prepare a statement for execution. We used the SELECT statement to query product data from the  products table based on a specified product code. We used question mark (?) as a placeholder for the product code.

Next, we declared a product code variable  @pc and set it values to S10_1678.

Then, we used the EXECUTE statement to execute the prepared statement with product code variable @pc.

Finally, we used the  DEALLOCATE PREPARE to release the prepared statement.

In this tutorial, we have shown you how to use MySQL prepared statement to execute a query with placeholders to improve the speed of the query and make your query more secure.

本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

prepared statement mysql_MySQL数据库之MySQL Prepared Statement相关推荐

  1. isfull mysql_MySQL数据库之MySQL 出现 The table is full 的解决方法

    本文主要向大家介绍了MySQL数据库之MySQL 出现 The table is full 的解决方法 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 当我们要写入新数据而发生& ...

  2. 无法打开数据库‘mysql_MySQL数据库之MYSQL无法启动解决方法

    本文主要向大家介绍了MySQL数据库之MYSQL无法启动解决方法 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 在my.ini(linux下/etc/my.cnf)加上skip ...

  3. ngram mysql_MySQL数据库之MySQL 全文检索 ngram Mybatis

    本文主要向大家介绍了MySQL数据库之MySQL 全文检索 ngram Mybatis ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 创建全文索引(FullText index ...

  4. JDBC:使用Statement操作数据库时产生的SQL注入问题原因分析

    SQL注入问题简单介绍 1.JDBC中使用Stement对数据库增删改查, 执行sql语句时使用拼接字符串会导致SQL注入 2.JDBC中使用PreaparedStement可以有效避免SQL注入问题 ...

  5. Java 中访问数据库的步骤?Statement 和PreparedStatement 之间的区别?

    Java 中访问数据库的步骤?Statement 和PreparedStatement 之间的区别? Java 中访问数据库的步骤 1)注册驱动: 2)建立连接: 3)创建Statement: 4)执 ...

  6. JDBC-使用Statement操作数据库的弊端

    使用Statement操作数据库的弊端 问题一:存在拼串操作 假设情景如下:当从控制台输入需要在数据库查询的用户名密码时,此时存在拼串操作,操作繁琐且可读性差 问题二:sql注入问题 所以为了避免sq ...

  7. A2-02-24.DML- Inserting Data into A Table Using MySQL INSERT Statement

    转载自:http://www.mysqltutorial.org/mysql-insert-statement.aspx Home / Basic MySQL Tutorial / Inserting ...

  8. mysql数据库有触发器吗_MySQL数据库之MySQL 触发器实现

    本文主要向大家介绍了MySQL数据库之MySQL 触发器实现 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. Summary: in this tutorial, you wil ...

  9. mysql入门优化_MySQL数据库:MySQL十大优化技巧详解

    本文主要向大家介绍了MySQL数据库的MySQL十大优化技巧详解,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. WEB开发者不光要解决程序的效率问题,对数据库的快速访问和相应也是 ...

最新文章

  1. error: No curses/termcap library found的解决办法
  2. 特权同学笔记-榨干FPGA片上存储资源
  3. 从 Nginx、Apache 工作原理看为什么 Nginx 比 Apache 高效!
  4. 领域驱动设计,让程序员心中有码(三)
  5. 【JS 逆向百例】元素ID定位加密位置,某麻将数据逆向
  6. JAVA入门级教学之(方法-2)
  7. html代码在线分析,网站html代码解析
  8. 牛逼!简单的代码提交能玩出这么多花样
  9. Memcached下载与安装
  10. Android图片的三级缓存整理
  11. FGSM实例:利用fgsm攻击RMB识别模型
  12. 多语言国家与缩写映射表
  13. 成为管理层必会的技能之一!利用Python打造一款员工管理系统~
  14. RFNoC GNU Radio使用教程
  15. 怎样在苹果Mac鼠标或触控板上复制和粘贴?
  16. 面试题:关于搭建测试环境 (一)
  17. 服务器换完主板后无盘进不了系统,NF主板集成网卡无盘无法启动的解决方案
  18. CoderHelper 是怎样实现添加模块信息以及函数说明信息的?
  19. 技术牛人---章文嵩博士---做系统要先了解业务的需求
  20. 蓝牙耳机连接苹果手机成功,但是手机音乐依然外放。

热门文章

  1. 人脸检测概述(不是人脸识别)
  2. 面向数据中台的数据治理七把利剑
  3. 这些使用工具大推荐,现在知道不晚
  4. 基于java的旅行社网站设计
  5. php获取文件mime类型,php 准确获取文件MIME类型的方法
  6. python进行图像边缘检测
  7. 智慧化工视频分析解决方案
  8. 2030年python入门必读的书籍【精心整理,PDF分享】
  9. 敏捷管理是PMP体系中的一部分,还是另一种体系?
  10. js每5秒发送一个请求 40s后结束