字符串值不正确:列'xEFxBFxBD'



我有一个表,我需要处理各种字符。字符包括Ø等®。

我已将表设置为 utf-8 作为默认排序规则,所有列都使用表默认值,但是当我尝试插入这些字符时,出现错误:字符串值不正确:第 1 行的"买家名称"列的字符串值"\xEF\xBF\xBD"

我的连接字符串定义为

string mySqlConn = "server="+server+";user="+username+";database="+database+";port="+port+";password="+password+";charset=utf8;";

我不知道为什么我仍然看到错误。我是否错过了 .net 连接器或我的 MySQL 设置的任何内容?

--编辑--

我的(新的)C# 插入语句如下所示:

MySqlCommand insert = new MySqlCommand( "INSERT INTO fulfilled_Shipments_Data " +
     "(amazonOrderId,merchantOrderId,shipmentId,shipmentItemId,"+
     "amazonOrderItemId,merchantOrderItemId,purchaseDate,"+ ...
      VALUES (@amazonOrderId,@merchantOrderId,@shipmentId,@shipmentItemId,"+
      "@amazonOrderItemId,@merchantOrderItemId,@purchaseDate,"+ 
      "paymentsDate,shipmentDate,reportingDate,buyerEmail,buyerName,"+ ...

       insert.Parameters.AddWithValue("@amazonorderId",lines[0]);
       insert.Parameters.AddWithValue("@merchantOrderId",lines[1]); 
       insert.Parameters.AddWithValue("@shipmentId",lines[2]);
       insert.Parameters.AddWithValue("@shipmentItemId",lines[3]);
       insert.Parameters.AddWithValue("@amazonOrderItemId",lines[4]);
       insert.Parameters.AddWithValue("@merchantOrderItemId",lines[5]);
       insert.Parameters.AddWithValue("@purchaseDate",lines[6]);
       insert.Parameters.AddWithValue("@paymentsDate",lines[7]);
 insert.ExecuteNonQuery();

假设这是使用参数化语句的正确方法,它仍然给出错误

 "Incorrect string value: 'xEFxBFxBD' for column 'buyerName' at row 1"

还有其他想法吗?

>xEFxBFxBD是Unicode字符U+FFFD的UTF-8编码。这是一个特殊字符,也称为"替换字符"。维基百科页面上关于特殊 unicode 字符的引用:

替换字符(通常是带有白色问号的黑色菱形)是 Unicode 标准中 Specials 表中代码点 U+FFFD 处的符号。它用于指示系统无法将数据流解码为正确符号时的问题。当字体不包含字符时最常见,但当数据无效且与任何字符都不匹配时也会出现:

因此,您的数据源似乎包含损坏的数据。也可能尝试使用错误的编码读取数据。线路从何而来?

如果您无法修复数据,并且您的输入确实包含无效字符,则可以删除替换字符:

lines[n] = lines[n].Replace("xFFFD", "");

Mattmanser是对的,永远不要通过在查询中直接连接参数来编写SQL查询。参数化查询的一个示例如下:

string lastname = "Doe";
double height = 6.1;
DateTime date = new DateTime(1978,4,18);
var connection = new MySqlConnection(connStr);
try
{
    connection.Open();
    var command = new MySqlCommand(
        "SELECT * FROM tblPerson WHERE LastName = @Name AND Height > @Height AND BirthDate < @BirthDate", connection);
    command.Parameters.AddWithValue("@Name", lastname);
    command.Parameters.AddWithValue("@Height", height);
    command.Parameters.AddWithValue("@Name", birthDate);
    MySqlDataReader reader = command.ExecuteReader();
    ...
}
finally
{
    connection.Close();
}
<</div> div class="one_answers">

对于那些在使用 PHP 时遇到类似问题的人,请尝试函数 utf8_encode($string) 。它只是工作!

当我的网站编码是utf-you并且我尝试以CP-1250字符串的形式发送时,我遇到了一些问题(listdir词典的示例)。我认为您必须发送像网站一样编码的字符串。

最新更新