show databases
create database 数据库名 default charset utf8 collate utf8_general_ci;
drop database 数据库名
use 数据库名
指令示例:
查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| day01db |
| employees |
| mysql |
| performance_schema |
| resume |
| sys |
| test |
+--------------------+
8 rows in set (0.03 sec)
创建数据库
-- charset:数据库编码 collate:数据库排序规则
mysql> create database day02db default charset utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
删除数据库
mysql> drop database day02db;
Query OK, 0 rows affected (0.04 sec)
进入数据库
mysql> use day01db;
Database changed
查看当前数据库下的数据表
mysql> show tables;
+-------------------+
| Tables_in_day01db |
+-------------------+
| l1 |
| tb1 |
| tb2 |
| tb3 |
| tb4 |
| tb5 |
+-------------------+
6 rows in set (0.00 sec)
退出数据库
mysql> exit;
Bye
首先需要我们下载一个Python的第三方库
pip install pymysql
连接数据库(数据库的链接使用的是socket)
import pymysql
# 连接数据库
# AttributeError: 'NoneType' object has no attribute 'encoding' 出现这个错误的原因是是因为 charset='utf-8' 数据库的utf-8数据集是utf8
conn = pymysql.connect(host='localhost', port=3306, password='20020115', user='root', charset='utf8')
# 创建游标
cursor = conn.cursor()
查看数据库(查询的结果是以元组的形式返回)
cursor.execute('show databases')
result = cursor.fetchall()
print(result)
(('information_schema',), ('day01db',), ('employees',), ('mysql',), ('performance_schema',), ('resume',), ('sys',), ('test',))
创建数据库
cursor.execute("create database if not exists day02db default charset utf8 collate utf8_general_ci")
# 讲前面的语句进行提交
conn.commit()
cursor.execute('show databases')
result = cursor.fetchall()
print(result)
删除数据库
cursor.execute('drop database day02db')
conn.commit()
cursor.execute('show databases')
result = cursor.fetchall()
print(result)
(('information_schema',), ('day01db',), ('employees',), ('mysql',), ('performance_schema',), ('resume',), ('sys',), ('test',))
进入数据库
cursor.execute('use day01db')
cursor.execute("show tables")
result = cursor.fetchall()
print(result)
(('l1',), ('tb1',), ('tb2',), ('tb3',), ('tb4',), ('tb5',))