MySQL之prepare用法,mysqlprepare

发布时间:2019-05-23  栏目:MySQL  评论:0 Comments

MySQL之prepare用法,mysqlprepare

MySQL官方将prepare、execute、deallocate统称为PREPARE
STATEMENT,习贯称其为【预管理语句】,上面是对其详细的牵线。

演示代码

PREPARE stmt_name FROM preparable_stmt

EXECUTE stmt_name
 [USING @var_name [, @var_name] ...] -

{DEALLOCATE | DROP} PREPARE stmt_name

 

举个栗子:

mysql> PREPARE pr1 FROM 'SELECT ?+?';
Query OK, 0 rows affected (0.01 sec)
Statement prepared

mysql> SET @a=1, @b=10 ;
Query OK, 0 rows affected (0.00 sec)

mysql> EXECUTE pr1 USING @a, @b;
+------+
| ?+? |
+------+
| 11 |
+------+
1 row in set (0.00 sec)

mysql> EXECUTE pr1 USING 1, 2; -- 只能使用用户变量传递。
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the 
right syntax to use near '1, 2' at line 1

mysql> DEALLOCATE PREPARE pr1;
Query OK, 0 rows affected (0.00 sec)

 

利用PAREPARE
STATEMENT可以收缩每一次实行SQL的语法分析,比方用来施行带有WHERE条件的SELECT和DELETE,恐怕UPDATE,或许INSERT,只须要每趟修改动量值就能够。

一致可防止守SQL注入,参数值能够分包转义符和定界符。

适用在应用程序中,大概SQL脚本中均可。

越多用法:

同1PREPARE … FROM能够壹直接用户变量:

mysql> CREATE TABLE a (a int);
Query OK, 0 rows affected (0.26 sec)

mysql> INSERT INTO a SELECT 1;
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO a SELECT 2;
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO a SELECT 3;
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> SET @select_test = CONCAT('SELECT * FROM ', @table_name);
Query OK, 0 rows affected (0.00 sec)

mysql> SET @table_name = 'a';
Query OK, 0 rows affected (0.00 sec)

mysql> PREPARE pr2 FROM @select_test;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE pr2 ;
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)

mysql> DROP PREPARE pr2; -- 此处DROP可以替代DEALLOCATE
Query OK, 0 rows affected (0.00 sec)

 

每贰回进行完EXECUTE时,养成好习于旧贯,须实施DEALLOCATE PREPARE …
语句,这样能够释放奉行中央银行使的富有数据库能源(如游标)。

不止如此,若是八个session的预管理语句过多,恐怕会达成max_prepared_stmt_count的上限值。

预管理语句只可以在创设者的对话中得以采用,其余会话是无力回天选用的。

同不时间在放肆格局(符合规律或不规则)退出会话时,以前定义好的预管理语句将不复存在。

设若在蕴藏进程中使用,假若不在进度中DEALLOCATE掉,在储存进度甘休之后,该预管理语句如故会立见功用。

http://www.bkjia.com/Mysql/1313006.htmlwww.bkjia.comtruehttp://www.bkjia.com/Mysql/1313006.htmlTechArticleMySQL之prepare用法,mysqlprepare
MySQL官方将prepare、execute、deallocate统称为PREPARE
STATEMENT,习贯称其为【预管理语句】,上面是对其详细的牵线。…

MySQL官方将prepare、execute、deallocate统称为PREPARE
STATEMENT,习于旧贯称其为【预管理语句】,上边是对其详细的介绍。

演示代码

PREPARE stmt_name FROM preparable_stmt

EXECUTE stmt_name
 [USING @var_name [, @var_name] ...] -

{DEALLOCATE | DROP} PREPARE stmt_name

 

举个栗子:

mysql> PREPARE pr1 FROM 'SELECT ?+?';
Query OK, 0 rows affected (0.01 sec)
Statement prepared

mysql> SET @a=1, @b=10 ;
Query OK, 0 rows affected (0.00 sec)

mysql> EXECUTE pr1 USING @a, @b;
+------+
| ?+? |
+------+
| 11 |
+------+
1 row in set (0.00 sec)

mysql> EXECUTE pr1 USING 1, 2; -- 只能使用用户变量传递。
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the 
right syntax to use near '1, 2' at line 1

mysql> DEALLOCATE PREPARE pr1;
Query OK, 0 rows affected (0.00 sec)

 

利用PAREPARE
STATEMENT能够减去每一趟推行SQL的语法剖判,比方用来施行带有WHERE条件的SELECT和DELETE,也许UPDATE,也许INSERT,只需求每一回修改动量值就能够。

同等可避防止SQL注入,参数值能够包括转义符和定界符。

适用在应用程序中,或许SQL脚本中均可。

越多用法:

同样PREPARE … FROM能够1直接用户变量:

mysql> CREATE TABLE a (a int);
Query OK, 0 rows affected (0.26 sec)

mysql> INSERT INTO a SELECT 1;
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO a SELECT 2;
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> INSERT INTO a SELECT 3;
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> SET @select_test = CONCAT('SELECT * FROM ', @table_name);
Query OK, 0 rows affected (0.00 sec)

mysql> SET @table_name = 'a';
Query OK, 0 rows affected (0.00 sec)

mysql> PREPARE pr2 FROM @select_test;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE pr2 ;
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)

mysql> DROP PREPARE pr2; -- 此处DROP可以替代DEALLOCATE
Query OK, 0 rows affected (0.00 sec)

 

每回实施完EXECUTE时,养成好习于旧贯,须实行DEALLOCATE PREPARE …
语句,那样能够释放试行中行使的保有数据库财富(如游标)。

不唯有如此,假诺1个session的预处理语句过多,只怕会落成max_prepared_stmt_count的上限值。

预管理语句只幸而创设者的对话中能够动用,别的会话是无法选用的。

同时在随便格局(不荒谬或不规则)退出会话时,以前定义好的预管理语句将消灭。

假定在蕴藏进程中采纳,即使不在进度中DEALLOCATE掉,在积累进度甘休以往,该预管理语句依然会使得。

留下评论

网站地图xml地图