LoginSignup
0
1

More than 5 years have passed since last update.

データベースを使ってみる

Last updated at Posted at 2017-09-11

初めてNode-REDを使ってみよう、というかたのためのガイドです。
Node-REDとMySQLを使ってシンプルなウェブサイトを作ってみます。

1. MySQLを起動し、ログインする

  • mysql.server startでMySQLサーバーを起動します
MySQLサーバーを起動

MacBook-Pro:~ tohru$ mysql.server start
Starting MySQL
 SUCCESS! 
MacBook-Pro:~ tohru$ 

  • mysql -u root -pでMySQLにログインします
MySQLへログイン

MacBook-Pro:~ tohru$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 Homebrew

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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> 

2. データベースを用意する

  • データベースを未作成の場合、create database データベース名;でデータベースを作成します。ここでは例として、「DB01」というデータベースを使用することにします。末尾の「;」を忘れないようにします。
データベースの作成

mysql> create database DB01;
Query OK, 1 row affected (0.00 sec)

  • もし間違えて作成してしまった場合、drop database db01;で作成してしまったデータベースを削除することが可能です。
データベースの削除

mysql> drop database db01;
Query OK, 0 rows affected (0.01 sec)

mysql> 

既にデータベース:DB01を作成済みの場合、use db01;で使用するデータベース「DB01」を指定します。

使用するデータベースを指定

mysql> use db01;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 

3. 表(テーブル)を用意する

  • 表(テーブル)を作成します。末尾の「;」を忘れないようにします。
CREATE文サンプル

CREATE TABLE TABLE01 (
    ID INT NOT NULL,
    NAME VARCHAR(100),
    C1 VARCHAR(20),
    C2 VARCHAR(20),
    C3 VARCHAR(20),
    C4 VARCHAR(20),
    C5 VARCHAR(20),
    I1 INT,
    I2 INT,
    I3 INT,
    I4 INT,
    I5 INT,
    PRIMARY KEY(ID)
);

表を作成

mysql> CREATE TABLE TABLE01 (
    -> ID INT NOT NULL,
    -> NAME VARCHAR(100),
    -> C1 VARCHAR(20),
    -> C2 VARCHAR(20),
    -> C3 VARCHAR(20),
    -> C4 VARCHAR(20),
    -> C5 VARCHAR(20),
    -> I1 INT,
    -> I2 INT,
    -> I3 INT,
    -> I4 INT,
    -> I5 INT,
    -> PRIMARY KEY(ID)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> 

  • 誤って作成してしまった表はdrop table table01;で削除可能です。
表を削除

mysql> drop table table01;
Query OK, 0 rows affected (0.01 sec)

mysql> 

4. 表にデータを書き込む

  • insert文で表へデータを書き込みます
  • insert into table01 (id, name, c1) values (1, "Hyper Text Transfer Protocol", "HTTP");で書き込みます
データを表へ書き込み(シンプル)

insert into table06 (MOJI_DATA, SUJI_DATA) values ("こんにちは", 1);

データを表へ書き込み(インサート)

mysql> insert into table01 (id, name, c1) values (1, "Hyper Text Transfer Protocol", "HTTP");
Query OK, 1 row affected (0.01 sec)

mysql> 

- 間違えて書き込んでしまった場合、detele文で削除します。
- delete from table01 where id = 1;で先ほど書き込んだ行を削除することができます。

行を削除

mysql> delete from table01 where id = 1;
Query OK, 1 row affected (0.01 sec)

mysql> 

5. 表のデータを読む

  • select文で表からデータを読み込みます。
  • select * from table01;で先ほど書き込んだデータを読み込みます。

mysql> select * from table01;                                                  +----+------------------------------+------+------+------+------+------+------+------+------+------+------+
| ID | NAME                         | C1   | C2   | C3   | C4   | C5   | I1   | I2   | I3   | I4   | I5   |
+----+------------------------------+------+------+------+------+------+------+------+------+------+------+
|  1 | Hyper Text Transfer Protocol | HTTP | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+----+------------------------------+------+------+------+------+------+------+------+------+------+------+
1 row in set (0.00 sec)

mysql> 

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1