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();
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'.
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?
Name is a reserved keyword in 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)";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.