1. 程式人生 > 其它 >MYSQL-庫操作

MYSQL-庫操作

目錄

資料庫管理

安裝上資料庫之後,就需要開始學習指令了,通過指令讓MySQL去做出一些檔案操作。

如果將資料庫管理系統與之前的檔案管理做類比的話:

資料庫管理系統 檔案管理
資料庫 資料夾
資料表 資料夾下的excel檔案

接下來,我們先學習 資料庫(資料夾)相關操作的指令。

內建客戶端操作

當連線上MySQL之後,執行如下指令(一般稱為SQL語句),就可以對MySQL的資料進行操作。

檢視當前所有的資料庫:show databases;

  • 建立資料庫:create database 資料庫名 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

    create database day25db;
    
    create database day25db DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
    
  • 刪除資料庫:drop database 資料庫名;

  • 進入資料(進入檔案):use 資料庫;

示例
# 1.登入MySQL
wupeiqi@wupeiqideMBP ~ % /usr/local/mysql/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.

# 2.檢視當前資料庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

# 3. 建立資料庫:  create database 資料庫名 default charset 編碼 collate 排序規則;
mysql> create database db1 default charset utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

# 4. 刪除資料庫
mysql> drop database db1;
Query OK, 0 rows affected (0.00 sec)

# 5. 檢視當前資料庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

# 6. 進入資料庫
mysql> use mysql;
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
# 7. 進入mysql資料庫(資料夾),檢視此資料庫下的所有表。
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.00 sec)

# 8. 退出
mysql>exit;

Python程式碼操作

無論通過何種方式去連線MySQL,本質上傳送的 指令 都是相同的,只是連線的方式和操作形式不同而已。

當連線上MySQL之後,執行如下指令,就可以對MySQL的資料進行操作。(同上述過程)

  • 檢視當前所有的資料庫 show databases;
  • 建立資料庫:create database 資料庫名 default charset utf8 collate utf8_general_ci;
  • 刪除資料庫:drop database 資料庫名;
  • 進入資料(進入檔案):use 資料庫;

想要使用Python操作MySQL需要安裝第三方模組:
pip3 install pymysql


安裝完成後,就可以編寫程式碼:

點選檢視程式碼
import pymysql

# 連線MySQL(socket)
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root123', charset="utf8")
cursor = conn.cursor()

# 1. 檢視資料庫
# 傳送指令
cursor.execute("show databases")
# 獲取指令的結果
result = cursor.fetchall()
print(result) # (('information_schema',), ('mysql',), ('performance_schema',), ('sys',))

# 2. 建立資料庫(新增、刪除、修改)
# 傳送指令
cursor.execute("create database db3 default charset utf8 collate utf8_general_ci")
conn.commit()

# 3. 檢視資料庫
# 傳送指令
cursor.execute("show databases")
# 獲取指令的結果
result = cursor.fetchall()
print(result) # (('information_schema',), ('db3',), ('mysql',), ('performance_schema',), ('sys',))

# 4. 刪除資料庫
# 傳送指令
cursor.execute("drop database db3")
conn.commit()

# 3. 檢視資料庫
# 傳送指令
cursor.execute("show databases")
# 獲取指令的結果
result = cursor.fetchall()
print(result) # (('information_schema',), ('mysql',), ('performance_schema',), ('sys',))

# 5. 進入資料庫,查看錶
# 傳送指令
cursor.execute("use mysql")
cursor.execute("show tables")
result = cursor.fetchall()
print(result) # (('columns_priv',), ('db',), ('engine_cost',), ('event',), ('func',), ('general_log',),....

# 關閉連線
cursor.close()
conn.close()