简体   繁体   中英

MySqlBackup.NET QueryExpress.ExecuteScalarStr() outputs strings enclosed in double-quotes instead of backticks

I'm using MySqlBackup.dll (MySqlBackup.NET) which in turn uses MySql.Data.dll to dump the database. I thought MySqlBackup.NET was causing this behavior, so I took it out of the equation. If I run this code in my solution:

Dim cmd = New MySqlCommand()
        cmd.Connection = New MySqlConnection(connectionString)
        cmd.Connection.Open()
        Dim result = QueryExpress.ExecuteScalarStr(cmd, "SHOW CREATE TABLE `airportcodes`;", 1)
        cmd.Connection.Close()

I get

CREATE TABLE "airportcodes" (
  "AirportCodeId" int(11) NOT NULL AUTO_INCREMENT,
  "Code" varchar(4) CHARACTER SET utf8 NOT NULL,
  "AirportName" varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  "Website" varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  "LastUpdate" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY ("AirportCodeId")
)

which I cannot use to restore because it uses double quotes. This happens when I use both the code above and MySqlBackup.NET. If I use the MySqlBackup.NET test application provided with its source code, the result is correct (uses backticks instead of double-quotes).

If I execute this query in the mysql CLI I also get the correct version (with backticks). I am using the same connection string all over.

It feels stupid to search-and-replace after the dump is created. What could be the cause of this?

This has nothing to do with MySqlBackup.NET

It is the behavior of MySql Server which can be configured either as default (GLOBAL) behavior or temporary (SESSION) behavior of MySQL server.

Here is the SQL statements that you can try out yourself:

Example 1: Export table structure with double quotes:

set session sql_mode=ANSI_QUOTES;
show create table `configkey`;

output:

CREATE TABLE "configkey" (
  "key" varchar(100) NOT NULL,
  "value" text,
  PRIMARY KEY ("key")
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Example 2: Export table structure with single quote

set session sql_mode=traditional;
show create table `configkey`;

output:

CREATE TABLE `configkey` (
  `key` varchar(100) NOT NULL,
  `value` text,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

For more information, please refer to official MySQL documentation under the topic: SQL-MODE https://dev.mysql.com/doc/refman/5.7/en/server-options.html#option_mysqld_sql-mode https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html

If you see the column names are wrapped with double quotes by default, it's most probably your MySQL server is configured to react in that way by default. You may consult your MySQL server administrator or provider.

Alternatively, you can configure the SQL_MODE each time manually before executing MySqlBackup.NET. Below is an example:

using (MySqlConnection conn = new MySqlConnection(constring))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            cmd.Connection = conn;
            conn.Open();

            cmd.CommandText = "set session sql_mode=traditional;";
            cmd.ExecuteNonQuery();

            mb.ExportToFile(file);
            conn.Close();
        }
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM