Created
April 17, 2011 15:01
-
-
Save nissuk/924101 to your computer and use it in GitHub Desktop.
MySQL: プリペアドステートメントの単純な例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- プリペアドステートメントを定義します。 | |
PREPARE s FROM 'SELECT * FROM users WHERE id = ?'; | |
-- プリペアドステートメントを実行します。 | |
-- (実行の構文が | |
-- EXECUTE stmt_name [USING @var_name [, @var_name] ...] | |
-- となっているのでリテラルは使えません) | |
SET @id = 1; | |
EXECUTE s USING @id; | |
-- プリペアドステートメントの割り当てを解除します。(DROP PREPARE s でも可) | |
DEALLOCATE PREPARE s; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- 上記プリペアドステートメントをテストするためあらかじめ定義しておくテーブルです。 | |
-- | |
DROP TABLE IF EXISTS users; | |
CREATE TABLE users ( | |
id INT UNSIGNED AUTO_INCREMENT COMMENT "内部ID", | |
name VARCHAR(255) NOT NULL COMMENT "氏名", | |
gender INT UNSIGNED NOT NULL COMMENT "性別", | |
PRIMARY KEY (id) | |
) ENGINE = InnoDB COMMENT = "ユーザー"; | |
INSERT INTO users(name, gender) values | |
('foo', 1), | |
('bar', 2), | |
('baz', 1) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment