golangã§DBæ¥ç¶ï¼MySQLï¼ã試ããã®ã§ãã®ãµã³ãã«ã§ãã
Â
以ä¸ãµã³ãã«ã
golang 㧠Mysql ã«æ¥ç¶
MySQL ãæºå
ã¾ãã¯æ¥ç¶å ã®MySQLç°å¢ãæºåãã¾ãã
ããã¯æã£åãæ©ããDocker ã§ç°å¢ãä½ãã¾ããã
ï¼åæï¼
Docker ãã¤ã³ã¹ãã¼ã«æ¸ã¿ã§ãããã¨
mysql-client ãã¤ã³ã¹ãã¼ã«æ¸ã¿ã§ãããã¨ï¼mysql ã³ãã³ãã使ç¨ã㦠DBæ¥ç¶ã確èªããããï¼
ä¸è¨ã® docker-compose.yml ãä½æãããdocker-compose up -dãã§èµ·åãã¾ãã
Â
[docker-compose.yml]
Â
mysql -h 127.0.0.1 --port 3306 -uroot -pmysql
ä¸è¨ã³ãã³ãã§MySQLã«æ¥ç¶ãã§ãã¾ãã
ããã§MySQLç°å¢æ§ç¯ã¯å®äºã§ãã
mysql -h 127.0.0.1 --port 3306 -uroot -pmysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Â Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPLCopyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>Â
Â
Goããã°ã©ã ãã MySQLã«æ¥ç¶
ç¶ãã¦ãGo ã§MySQL ã«æ¥ç¶ããå¦çãæ¸ãã¦ããã¾ãã
 Go-MySQL-Driver ã©ã¤ãã©ãªã使ç¨ãã¾ããã
Â
ä¸è¨ã§å種ãã¡ã¤ã«ãç¨æãã¾ãã
mkdir sample
go mod init sample
touch main.go
Â
ã¾ã㯠main.go ã«ã¯ä¸è¨ã®å 容ãè¨è¼ãã¾ãã
Â
Â
ãgithub.com/go-sql-driver/mysqlãã®ã¨ããã§ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã®ã§ã
go mod tidy
ãå®è¡ãããã¨ã§ãgo-sql-driver ã go.mod ã®ä¾åé¢ä¿ã«è¿½å ããã¾ããï¼go.mod ãã¡ã¤ã«ãæ´æ°ãããï¼
Â
â» MySQLã«ã¯äºåã«sampleãã¼ã¿ãã¼ã¹ãä½ã£ã¦ããã¾ããï¼ãCREATE DATABASE IF NOT EXISTS sample;ã ãå®è¡ï¼
show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mysql        |
| performance_schema |
| sample       |
| sys         |
+--------------------+
5 rows in set (0.01 sec)
Â
ããã¾ã§ã§ããããmain.go ãå®è¡ãã¾ãã
ãDBæ¥ç¶ãã¨è¡¨ç¤ºãããã°ãåé¡ãªãæ¥ç¶ãã§ãã¦ãã¾ãã
go run main.goÂ
DBæ¥ç¶
Â
MySQLã¸ã®CRUDå¦çã追å
æ¥ç¶ããã ãã ã¨é¢ç½ããªãã®ã§ãDBã¸ã®CRUDã試ãã¾ãã
Â
ã¾ãã¯åä½æ¤è¨¼ç¨ã®ãã¼ãã«ãç¨æã
use samle;
CREATE TABLE user (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255),
  age INT
);
Â
MySQLã¸ãã¼ã¿ç»é²
ç¶ãã¦ãã¼ã¿ã®ç»é²ã§ãã
ãã¼ã¿ç»é²ç¨ã® dbInsert é¢æ°ã追å ãã¦ãã¾ãã
Â
Â
ã³ãã³ãã©ã¤ã³ãããinsert/update/delete/select ã®ããããã®å ¥åãåãä»ãã
insert ã®å ´åã«ãã¼ã¿ç»é²ãè¡ãããã«ãã¦ãã¾ãã
ãããå®è¡ãã¦ã¿ã¾ãã
go run main.goÂ
DBæ¥ç¶
ã³ãã³ããå ¥å (insert/update/delete/select): insert
ååãå ¥å: test
å¹´é½¢ãå ¥å: 10
追å ãã¾ããã
追å ãããã¬ã³ã¼ãã®ID: 1, åå: test, å¹´é½¢: 10
Â
MySQLã«ãSelectå¥ãæãã¦ã¿ã¾ãã
select * from user;
+----+------+------+
| id | name | age  |
+----+------+------+
| Â 1 | test | Â 10 |
+----+------+------+
1 row in set (0.00 sec)
Â
ç¡äºã«ç»é²ã§ãã¦ãã¾ãã
Â
MySQLã¸ãã¼ã¿æ´æ°
ç¶ãã¦ãã¼ã¿ã®æ´æ°ã§ãã
ãã¼ã¿æ´æ°ç¨ã® dbUpdate é¢æ°ã追å ãã¦ãã¾ãã
Â
Â
ã³ãã³ãã©ã¤ã³ããã®å ¥åããupdate ã®å ´åã«ãã¼ã¿æ´æ°ãè¡ãããã«ãã¦ãã¾ãã
ãããå®è¡ãã¦ã¿ã¾ãã
go run main.goÂ
DBæ¥ç¶
ã³ãã³ããå ¥å (insert/update/delete/select): update
æ´æ°å¯¾è±¡ã®ã¬ã³ã¼ãã®IDãå ¥å: 1
æ°ããååãå ¥å: test_update
æ°ããå¹´é½¢ãå ¥å: 20
æ´æ°ãã¾ããã
æ´æ°ãããã¬ã³ã¼ãã®ID: 1, æ°ããåå: test_update, æ°ããå¹´é½¢: 20
Â
MySQLã«ãSelectå¥ãæãã¦ã¿ã¾ãã
select * from user where id = 1;
+----+-------------+------+
| id | name     | age  |
+----+-------------+------+
| Â 1 | test_update | Â 20 |
+----+-------------+------+
1 row in set (0.00 sec)
Â
ç¡äºã«æ´æ°ã§ãã¦ãã¾ãã
Â
MySQLã¸ãã¼ã¿æ¤ç´¢
ç¶ãã¦ãã¼ã¿ã®æ¤ç´¢ã§ãã
ãã¼ã¿æ¤ç´¢ç¨ã® dbSelect é¢æ°ã追å ãã¦ãã¾ãã
Â
Â
ã³ãã³ãã©ã¤ã³ããã®å ¥åããselect ã®å ´åã«ãã¼ã¿æ¤ç´¢ãè¡ãããã«ãã¦ãã¾ãã
ãããå®è¡ãã¦ã¿ã¾ãã
go run main.goÂ
DBæ¥ç¶
ã³ãã³ããå ¥å (insert/update/delete/select): select
æ¤ç´¢å¯¾è±¡ã®ã¬ã³ã¼ãã®IDãå ¥å: 1
ID: 1, åå: test_update, å¹´é½¢: 20
Â
MySQLã«ãSelectå¥ãæãã¦ã¿ã¾ãã
select * from user where id = 1;
+----+-------------+------+
| id | name     | age  |
+----+-------------+------+
| Â 1 | test_update | Â 20 |
+----+-------------+------+
1 row in set (0.00 sec)
Â
ç¡äºã«æ¤ç´¢ã§ãã¦ãã¾ãã
Â
MySQLã¸ãã¼ã¿åé¤
ç¶ãã¦ãã¼ã¿ã®åé¤ã§ãã
ãã¼ã¿åé¤ç¨ã® dbDelete é¢æ°ã追å ãã¦ãã¾ãã
Â
Â
ã³ãã³ãã©ã¤ã³ããã®å ¥åããdelete ã®å ´åã«ãã¼ã¿åé¤ãè¡ãããã«ãã¦ãã¾ãã
ãããå®è¡ãã¦ã¿ã¾ãã
go run main.goÂ
DBæ¥ç¶
ã³ãã³ããå ¥å (insert/update/delete/select): delete
åé¤å¯¾è±¡ã®ã¬ã³ã¼ãã®IDãå ¥å: 1
åé¤ãã¾ããã
Â
MySQLã«ãSelectå¥ãæãã¦ã¿ã¾ãã
mysql> select * from user where id = 1;
Empty set (0.00 sec)
Â
ç¡äºã«åé¤ã§ãã¦ãã¾ãã
Â
以ä¸ãµã³ãã«ã«ãªãã¾ãã
Â