错误 SQL查询:

--
-- Database: `work`
--
-- --------------------------------------------------------
--
-- Table structure for table `administrators`
--
CREATE TABLE IF NOT EXISTS `administrators` (

`user_id` varchar( 30 ) NOT NULL ,
`password` varchar( 30 ) NOT NULL ) ENGINE = InnoDB DEFAULT CHARSET = latin1;

MySQL说:

#1046 - No database selected

这里需要帮助。


当前回答

我想我迟到了抱歉

如果你在这里和我一样,当这个错误发生在mysqldump而不是mysql时,尝试这个解决方案,我在一个德国网站上偶然发现的,所以我想和像我一样头疼的无家可归的人分享。

因此,问题的发生是因为在数据库名称之前缺少-databases参数

所以你的命令必须是这样的:

mysqldump -pdbpass -udbuser --databases dbname

在我的情况下,问题的另一个原因是,我在本地开发和根用户没有密码,所以在这种情况下,你必须使用——password=而不是-pdbpass,所以我的最终命令是:

mysqldump -udbuser --password= --databases dbname

链接到完整的线程(德语):https://marius.bloggt-in-braunschweig.de/2016/04/29/solution-mysqldump-no-database-selected-when-selecting-the-database/

其他回答

假设你正在使用命令行:

1. 找到数据库

显示数据库;

2. 从列表中选择一个数据库

使用经典模型;你应该去参加比赛!(显然,您必须在列表中使用正确命名的数据库。

为什么会出现这个错误?

Mysql要求您选择正在使用的特定数据库。我认为这是他们做出的一个设计决策:它避免了很多潜在的问题:例如,你完全有可能在多个数据库中使用相同的表名,例如一个users表。为了避免这类问题,他们可能会想:“让用户选择他们想要的数据库”。

首先选择数据库:使用db_name

然后创建表:CREATE table tb_name ( id int, 名字varchar (255), 工资int, 城市varchar (255) );

这是mysql 5.5版本语法

如果你通过phpMyAdmin这样做:

I'm assuming you already Created a new MySQL Database on Live Site (by live site I mean the company your hosting with (in my case Bluehost)). Go to phpMyAdmin on live site - log in to the database you just created. Now IMPORTANT! Before clicking the "import" option on the top bar, select your database on the left side of the page (grey bar, on the top has PHP Myadmin written, below it two options:information_schema and name of database you just logged into. once you click the database you just created/logged into it will show you that database and then click the import option.

这招对我很管用。希望能有所帮助

当我试图导入以前创建的数据库时,我遇到了同样的错误。以下是我解决这个问题的方法:

创建新的数据库

2—使用Use命令

3-再试一次

这对我很有用。

MySQL Workbench

通过鼠标右键单击从模式选项卡选择数据库。 将数据库设置为默认模式