How to check if mysql database exists(如何检查mysql数据库是否存在)
问题描述
是否可以在建立连接后检查(MySQL)数据库是否存在.
Is it possible to check if a (MySQL) database exists after having made a connection.
我知道如何检查数据库中是否存在表,但我需要检查数据库是否存在.如果不是,我必须调用另一段代码来创建它并填充它.
I know how to check if a table exists in a DB, but I need to check if the DB exists. If not I have to call another piece of code to create it and populate it.
我知道这一切听起来有点不雅 - 这是一个快速而肮脏的应用程序.
I know this all sounds somewhat inelegant - this is a quick and dirty app.
推荐答案
SELECT SCHEMA_NAME
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'DBName'
如果您只需要知道一个数据库是否存在以便在尝试创建它时不会出错,只需使用 (From 此处):
If you just need to know if a db exists so you won't get an error when you try to create it, simply use (From here):
CREATE DATABASE IF NOT EXISTS DBName;
这篇关于如何检查mysql数据库是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查mysql数据库是否存在


- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- SQL 临时表问题 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01