【问题标题】:How to disable strict mode at MySql in Visual studio?如何在 Visual Studio 的 MySql 中禁用严格模式?
【发布时间】:2018-01-27 12:52:35
【问题描述】:

我最近尝试在远程服务器上将 MySql 与外部数据库一起使用,但每当我尝试在 Text/Blob 上设置默认值时,我都会收到一条错误消息。

MySql.Data.MySqlClient.MySqlException: 'BLOB/TEXT column 'prefix' can't have 
a default value'

显然问题在于仅在 Windows 上出现的错误...(?)

我在 Visual Studio 环境中使用 C#。 我使用 Nuget 获得了 MySql 包。但我没有看到在线指南告诉我如何在 Visual Studio 环境中禁用严格模式。

我的计算机上没有任何 my.ini 文件,因为我没有安装任何 Mysql 文件,如上所述,我使用 Nuget 为我的项目获取包。

如果可能,您能告诉我如何禁用允许我在 Text/Blob 上使用默认值的严格模式吗?

编辑: 对于那些想在这里查看 sql 行的人,它是:

String sql = "CREATE TABLE IF NOT EXISTS servers_preferneces (server_id TEXT,prefix TEXT DEFAULT '/', days_to_be_missing INT DEFAULT 3, excused_rank_name TEXT DEFAULT 'AWOL', " +
        "excused_rank_id TEXT DEFAULT '', missing_rank_name TEXT DEFAULT 'MIA', missing_rank_id TEXT DEFAULT NULL, welcome_state INTEGER DEFAULT 0, welcome_message TEXT DEFAULT ''," +
        " welcome_channel_id TEXT DEFAULT 'DEFAULT'); "; 

假设使用以下代码行执行:

MySqlCommand command = new MySqlCommand(sql, db_connection);
MySqlDataReader reader;
reader = command.ExecuteReader();

【问题讨论】:

  • 什么错误信息?
  • SHOW VARIABLES LIKE 'sql_mode' 命令的输出是什么;
  • @Leonardo 它显示空白作为值。这里发生了一些可疑的事情......
  • 您要执行的 SQL 语句是什么?你能显示一些代码吗?

标签: c# mysql visual-studio-2017 nuget-package


【解决方案1】:

MySQL 服务器不支持 TEXTBLOB 列的 DEFAULT 值;这是数据库引擎的known issue

如果服务器在严格模式下运行,这将产生您所看到的错误。如果服务器不是在严格模式下,它只是一个警告。

但是,即使您在服务器上关闭了严格模式,您的代码仍然无法正常工作:插入的行将使用 NULLs,而不是您指定的 DEFAULT 值。

解决方法是将架构更改为不包含 DEFAULT 值,并在 INSERT 语句中显式插入所需的默认值。

-- SQL
CREATE TABLE IF NOT EXISTS servers_preferneces
(
    server_id TEXT,
    prefix TEXT,
    days_to_be_missing INT DEFAULT 3,
    excused_rank_name TEXT,
    excused_rank_id TEXT,
    missing_rank_name TEXT,
    missing_rank_id TEXT,
    welcome_state INTEGER DEFAULT 0,
    welcome_message TEXT,
    welcome_channel_id TEXT );

// C#
using (var command = connection.CreateCommand())
{
    command.CommandText = @"INSERT INTO servers_preferneces(prefix,
        excused_rank_name, excused_rank_id, missing_rank_name
        welcome_message, welcome_channel_id)
        VALUES(@prefix, @excused_rank_name, @excused_rank_id,
        @missing_rank_name, @welcome_message, @welcome_channel_id);";
    command.Parameters.AddWithValue("@prefix", "/");
    command.Parameters.AddWithValue("@excused_rank_name", "AWOL");
    command.Parameters.AddWithValue("@excused_rank_id", "");
    command.Parameters.AddWithValue("@missing_rank_name", "MIA");
    command.Parameters.AddWithValue("@welcome_message", "");
    command.Parameters.AddWithValue("@welcome_channel_id", "DEFAULT");
    // add other parameters

    command.ExecuteNonQuery();
}

【讨论】:

    猜你喜欢
    • 2021-12-14
    • 2016-10-24
    • 2022-11-16
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 2019-12-14
    • 1970-01-01
    相关资源
    最近更新 更多