简体   繁体   English

字段名称没有默认值

[英]Field name does not have a default value

I'm trying to insert a record inside the competition table which have this structure: 我正在尝试在competition表中插入具有以下结构的记录:

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.dll中出现类型'MySQL.Data.MysqlClient.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? 现在competition表的字段name被设置为NOT NULL因此它没有默认值,但是我向命令传递了一个正确的值,所以我不明白为什么会显示这个错误,任何想法?

Name is a reserved keyword in MySql Name是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)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 int字段的默认值是0吗? - Does an int field have a default of 0? 字段“ FIELD”从未分配,并且将始终具有其默认值 - field 'FIELD' is never assigned to, and will always have its default value 动作参数是否具有默认值? - Does an action parameter have a default value? 如果字段类型没有默认构造函数,如何使用ValueResolver? - How to use ValueResolver if field type does not have a default constructor? 字段modifyDate永远不会分配给,并且始终具有其默认值0 - Field modifyDate is never assigned to, and will always have its default value 0 构造函数DI - Field永远不会分配给,并且始终具有其默认值 - Constructor DI - Field is never assigned to, and will always have its default value 字段永远不会分配给,并且始终具有默认值0 - Field is never assigned to and will always have its default value 0 字段从未分配给它,并且将始终具有其默认值 - Field is never assigned to, and will always have its default value 警告:永远不会将字段分配给,并且始终将其默认值设置为null - Warning: Field is never assigned to, and will always have its default value null 永远不会分配字段,并且始终具有默认值null - Field is never assigned and will always have default value null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM