2

我想传入一个 int 值并返回一个小于这个 int 的短密码列表。我在修改 MySQL 游标示例时遇到了麻烦:http: //dev.mysql.com/doc/refman/5.0/en/cursors.html。我想用短密码打印出行,但任何时候结果有不止一行 PhpMyAdmin 只会无限期地显示“处理请求”。我究竟做错了什么?它破坏了我周六的生产力!

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ShortPasswd`(IN `passwordLength` TINYINT)
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE a INT;
  DECLARE b VARCHAR(255);
  DECLARE cur1 CURSOR FOR SELECT UID, Password
      FROM authentication WHERE CHARACTER_length(Password) < passwordLength;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  OPEN cur1;

  read_loop: LOOP
    FETCH cur1 INTO a, b;
    IF done THEN
      LEAVE read_loop;
    END IF;
    SELECT b;
  END LOOP;

  CLOSE cur1;
END
$$
DELIMITER ;
4

2 回答 2

2
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ShortPasswd`(IN `passwordLength` TINYINT)
BEGIN

   SELECT UID, Password
      FROM authentication WHERE CHARACTER_length(Password) > passwordLength;

END
$$
DELIMITER ;

您将直接获得行 UID、密码。当你从查询中得到

call ShortPasswd;
于 2013-04-20T14:31:56.470 回答
0

我建议修改这些行:

...
DECLARE FINISHED BOOLEAN DEFAULT 0;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

...
IF done THEN
  CLOSE cur1;
  LEAVE read_loop;
END IF;

...
END LOOP read_loop;

我使用 0/1 而不是 FALSE/TRUE,并在离开 LOOP 之前关闭光标。

于 2013-04-20T14:25:07.423 回答