如何在mysql LOAD DATA INFILE查询中使用unicode字符作为字段分隔符

时间:2015-06-17 22:08:31

标签: mysql unicode

我正在尝试使用下面的查询将文件导入MySql数据库。文件中的字段由“|”(断开的垂直条)U + 00A6分隔。 我正在从PhpMyAdmin执行查询

LOAD DATA LOCAL INFILE "/path/filename.csv" INTO TABLE tablename 
CHARACTER SET 'UTF8'
FIELDS TERMINATED BY '¦' 

不知何故,mysql忽略字段分隔符并将所有记录加载到表的第一个字段

3 个答案:

答案 0 :(得分:0)

源文档可能以不需要的字符编码,例如Excel的功能。尝试在TextEdit或其他裸骨文本编辑器中打开并保存,确保数据保存为UTF-8。然后:

LOAD DATA LOCAL INFILE "filepath/file.csv" INTO TABLE `table`
CHARACTER SET 'utf8'
FIELDS TERMINATED BY '\¦';

在破坏栏前使用转义字符。还要对整个表使用UTF-8 Unicode编码,而不仅仅是行。

答案 1 :(得分:0)

您可以将其表示为LOAD DATA LOCAL INFILE "/path/filename.csv" INTO TABLE tablename CHARACTER SET 'UTF8' FIELDS TERMINATED BY X'A6'

// Production steps of ECMA-262, Edition 5, 15.4.4.14
// Reference: http://es5.github.io/#x15.4.4.14
if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(searchElement, fromIndex) {

    var k;

    // 1. Let O be the result of calling ToObject passing
    //    the this value as the argument.
    if (this == null) {
      throw new TypeError('"this" is null or not defined');
    }

    var O = Object(this);

    // 2. Let lenValue be the result of calling the Get
    //    internal method of O with the argument "length".
    // 3. Let len be ToUint32(lenValue).
    var len = O.length >>> 0;

    // 4. If len is 0, return -1.
    if (len === 0) {
      return -1;
    }

    // 5. If argument fromIndex was passed let n be
    //    ToInteger(fromIndex); else let n be 0.
    var n = +fromIndex || 0;

    if (Math.abs(n) === Infinity) {
      n = 0;
    }

    // 6. If n >= len, return -1.
    if (n >= len) {
      return -1;
    }

    // 7. If n >= 0, then Let k be n.
    // 8. Else, n<0, Let k be len - abs(n).
    //    If k is less than 0, then let k be 0.
    k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

    // 9. Repeat, while k < len
    while (k < len) {
      // a. Let Pk be ToString(k).
      //   This is implicit for LHS operands of the in operator
      // b. Let kPresent be the result of calling the
      //    HasProperty internal method of O with argument Pk.
      //   This step can be combined with c
      // c. If kPresent is true, then
      //    i.  Let elementK be the result of calling the Get
      //        internal method of O with the argument ToString(k).
      //   ii.  Let same be the result of applying the
      //        Strict Equality Comparison Algorithm to
      //        searchElement and elementK.
      //  iii.  If same is true, return k.
      if (k in O && O[k] === searchElement) {
        return k;
      }
      k++;
    }
    return -1;
  };
}

答案 2 :(得分:0)

SET NAMES utf8;   -- You were probably missing this (or the equivalent)
LOAD DATA LOCAL INFILE "/path/filename.csv" INTO TABLE tablename 
    CHARACTER SET 'UTF8'
    FIELDS TERMINATED BY '¦' 

(假设您可以输入utf8,而不是latin1)

线索为"Â" gibrish character at the end of every sting,同时¦ mojibaked变为¦