字段名没有默认值

时间:2022-11-14 23:46:15

I'm trying to insert a record inside the competition table which have this structure:

我想在竞争表中插入一个记录,它的结构如下:

CREATE TABLE IF NOT EXISTS `mydb`.`competition` (
  `id` INT NOT NULL,
  `country_id` INT NULL,
  `name` VARCHAR(255) NOT NULL,
  `link` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `id_idx` (`country_id` ASC),
  CONSTRAINT `FK_country_competition_country_id`
    FOREIGN KEY (`country_id`)
    REFERENCES `mydb`.`country` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

my code have the following design:

我的代码有以下设计:

 using (MySqlConnection connection = new DBConnection().Connect())
 {
     using (MySqlCommand command = new MySqlCommand())
     {
         command.Connection = connection;           
         command.CommandText = "INSERT INTO competition (id, country_id, name, link) " +
                        "VALUES (@id, @country_id, @name, @link)";

         command.Parameters.AddWithValue("@id", 840);
         command.Parameters.AddWithValue("@country_id", 2);
         command.Parameters.AddWithValue("@name", "Superliga");
         command.Parameters.AddWithValue("@link", "https://int.soccerway.com/national/albania/super-league/20172018/regular-season/r42054/");
         command.ExecuteNonQuery();
       }
  }

when the code reach this line: command.ExecuteNonQuery();

当代码到达这一行:command.ExecuteNonQuery();

I get:

我得到:

Exception has occurred: CLR/MySQL.Data.MySqlClient.MySqlException An exception of type 'MySQL.Data.MysqlClient.MySqlException' occurred in MySQL.Data.dll but was not handled in user code: 'field 'name' doesn't have a default value'.

异常发生:CLR / MySQL.Data.MySqlClient。MySqlException类型“MySQL.Data.MysqlClient”的异常。发生在MySQL.Data MySqlException”。但未在用户代码中处理:'field 'name'没有默认值'。

Now the field name of the competition table is setted as NOT NULL so it doesn't have a default value, but I passed to the command a correct value so I don't understand why this error is displayed, any idea?

现在竞争表的字段名被设置为NOT NULL,所以它没有默认值,但是我给命令传递了一个正确的值,所以我不明白为什么会显示这个错误,明白吗?

1 个解决方案

#1


3  

Name is a reserved keyword in MySql

名称是MySql中保留的关键字。

The best action is to change that column to a different text, but if you really want to use that then you need to put backticks around that identifier

最好的做法是将该列更改为另一个文本,但是如果您真的想使用它,那么需要在该标识符周围添加回勾

command.CommandText = @"INSERT INTO competition (id, country_id, `name`, link)
                        VALUES (@id, @country_id, @name, @link)";

#1


3  

Name is a reserved keyword in MySql

名称是MySql中保留的关键字。

The best action is to change that column to a different text, but if you really want to use that then you need to put backticks around that identifier

最好的做法是将该列更改为另一个文本,但是如果您真的想使用它,那么需要在该标识符周围添加回勾

command.CommandText = @"INSERT INTO competition (id, country_id, `name`, link)
                        VALUES (@id, @country_id, @name, @link)";