本文概述
- 获取现有数据库列表
- 创建新数据库
获取现有数据库列表我们可以使用以下MySQL查询来获取所有数据库的列表。
>
show databases;
例子
import mysql.connector#Create the connection object myconn = mysql.connector.connect(host = "localhost", user = "root", passwd = "google")#creating the cursor objectcur = myconn.cursor()try:dbs = cur.execute("show databases")except:myconn.rollback()for x in cur:print(x)myconn.close()
输出
('EmployeeDB', )('Test', )('TestDB', )('information_schema', )('srcmini', )('srcmini1', )('mydb', )('mysql', )('performance_schema', )('testDB', )
创建新数据库可以使用以下SQL查询创建新数据库。
>
create database <
database-name>
例子
import mysql.connector#Create the connection object myconn = mysql.connector.connect(host = "localhost", user = "root", passwd = "google")#creating the cursor objectcur = myconn.cursor()try:#creating a new databasecur.execute("create database PythonDB2")#getting the list of all the databases which will now include the new database PythonDBdbs = cur.execute("show databases")except:myconn.rollback()for x in cur:print(x)myconn.close()
输出
('EmployeeDB', )('PythonDB', )('Test', )('TestDB', )('anshika', )('information_schema', )('srcmini', )('srcmini1', )('mydb', )('mydb1', )('mysql', )('performance_schema', )('testDB', )
推荐阅读
- Python MySQL数据库连接如何操作()
- Python如何使用继承()
- Python魔术方法介绍
- python|用Python制作可视化报表,这也太快了
- 可视化|50套可视化报表模板直接用,做报告不用愁了!快收藏
- 一篇文章搞懂android存储目录结构
- rime 同文输入法 安卓系统
- Arguments 对象call()与apply()
- (十四)Android NDK混淆