3

我尝试将 MySQL 数据库与我在 Google Drive 中的文档同步。我在 Google App Script 上写了一个小脚本来完成这项工作。

我在处理字符编码时遇到了一些问题(默认字符集是 UTF-8,带有整理“general_ci”)。我尝试了不同的方法(encodeURIComponent、小型 UTF-8 工具类、Blob,...),但没有任何效果。这是小示例代码:

var title = result.getString("title");
title = Utilities.newBlob(title, Utilities.Charset.UTF_8);
if(title.getDataAsString() != file.getName()) {
          Logger.log('Updating title for document %s from %s to %s', file.getId(), title.getDataAsString(),          file.getName());
          stmt_update_title.setObject(2, file.getId());
          stmt_update_title.setObject(1, Utilities.newBlob(file.getName(), Utilities.Charset.UTF_8).getDataAsString());
          stmt_update_title.addBatch();
        }

当我从 MySQL 中检索时,我得到一个带有“?”的不正确字符串 而不是重音字符('é'、'è'、'à')。当我更新时,字符串已损坏(但它在日志中打印得很好)。

提前感谢您的帮助。

4

1 回答 1

3

您需要为 JDBC 对象使用特殊的连接字符串,例如

jdbc:mysql://host:port/instance?useUnicode=true&characterEncoding=UTF-8
于 2014-01-20T17:17:23.260 回答