show databases
CREATE TABLE 数据库 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
使用utf8编码,避免中文汉字问题。COLLATE 影响排序规则。
DROP DATABASE 数据库名
CREATE DATABASE day25db DEFAULT CHARSET utf8 COLLATE utf8_general_ci; Query OK, 1 row affected (0.01 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | day25db | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
DROP DATABASE day25db;
Query OK, 0 rows affected (0.02 sec)
use 数据库名
show tables
mysql> use information_schema Database changed mysql> show tables; +---------------------------------------+ | Tables_in_information_schema | +---------------------------------------+ | CHARACTER_SETS | | COLLATIONS | | COLLATION_CHARACTER_SET_APPLICABILITY | | INNODB_SYS_FOREIGN | | INNODB_SYS_TABLESTATS | +---------------------------------------+
#安装pymysql模块 LeodeMBP:~ leo$ pip3 install pymysql Collecting pymysql Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.8/43.8 KB 67.5 kB/s eta 0:00:00 Installing collected packages: pymysql Successfully installed pymysql-1.0.2 #导入pymysql import pymysql #建立数据库链接对象至mysql数据库 conn = pymysql.connect(host = '127.0.0.1' , port = 3306, user = 'root' , passwd = 'root123' , charset = 'utf8') #建立游标对象,基于游标可以发送指令。 cursor = conn.cursor() #查看系统的数据库 cursor.execute('show databases') result = cursor.fetchall() print(result) (('information_schema',), ('day25db',), ('mysql',), ('performance_schema',), ('sys',))
#创建数据库
cursor.execute("CREATE DATABASE day25db DEFAULT CHARSET utf8 COLLATE utf8_general_ci")
conn.commit() #提交事务来保存更改
#删除数据库
cursor.execute("DROP DATABASE day25db")
conn.commit()
#进入数据库,查看所有表
cursor.execute("use mysql")
cursor.execute("show tables")
result = cursor.fetchall()
print(result)
#关闭链接
cursor.close()
conn.close()