[英]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.