新建GuiDBConfig.py文件(确保外界不能访问以确保安全),保存数据库配置
# -*- coding: utf-8 -*-
dbConfig = {
'user': 'tiger',
'password': 'tiger',
'host': '127.0.0.1'
}
创建连接数据库文件,执行两次创建数据库操作,第二次显示错误(Failed to create DB: 1007 (HY000): Can't create database 'GuiDB'; database exists)
# -*- coding: utf-8 -*-
import GuiDBConfig as guiConf
import mysql.connector as mysql
# unpack dictionary credentials
conn = mysql.connect(**guiConf.dbConfig)
cursor = conn.cursor()
GUIDB = 'GuiDB'
try:
# 创建数据库
# cursor.execute("CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(GUIDB))
# 显示数据库
cursor.execute("show databases")
except mysql.Error as error:
print("Failed to create DB: {}".format(error))
print(cursor.fetchall())
cursor.close()
conn.close()
print(conn)
执行show databases显示结果:
[('information_schema',), ('GuiDB',), ('bladepro',), ('blog',), ('engineer',), ('ip',), ('mysql',), ('performance_schema',), ('spring_blog',), ('sys',), ('weixin',)]
mysql版本5.7,数据库名称区分大小写