【发布时间】:2020-03-26 23:21:53
【问题描述】:
我正在尝试使用 LiquiBase 执行以下插入:
INSERT INTO table (id, value) VALUES (0, 'nullás')
//
这显然是一个随机值,目标是将非英语的重音字符插入到 VARCHAR2 列中。目标是 Oracle 12c。如果我从我的 PC 通过 SQL Developer 手动运行插入,它可以正常工作。但是当我在同一台 PC 上通过 LiquiBase 运行它时,它会产生以下结果:
null�s
执行日志:
INSERT INTO table (id, value) VALUES (0, 'null?s')
插入本身包含在一个单独的 SQL 文件中。它被添加到变更集文件中,如下所示:
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="1" author="aaa" >
<comment>insert.sql</comment>
<sqlFile dbms="oracle" encoding="UTF-8" path="insert.sql" relativeToChangelogFile="true" endDelimiter="//" />
</changeSet>
以前编码设置为“utf8”,我认为这是一个错字,但将其更改为“UTF-8”后问题就出现了。相关 POM 设置:
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aaa</groupId>
<artifactId>aaa</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<version.ojdbc>12.2.0.1.0</version.ojdbc>
<version.org.liquibase.liquibase-maven-plugin>3.6.2</version.org.liquibase.liquibase-maven-plugin>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<liquibase.url>jdbc:oracle:thin:@1.2.3.4:1521/aaa</liquibase.url>
<!-- liquibase.url>offline:oracle?outputLiquibaseSql=true</liquibase.url -->
<liquibase.execute.goal>update</liquibase.execute.goal>
<liquibase.driver>oracle.jdbc.OracleDriver</liquibase.driver>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${version.org.liquibase.liquibase-maven-plugin}</version>
<configuration>
<propertyFile>liquibase.properties</propertyFile>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
<outputFileEncoding>UTF-8</outputFileEncoding>
<systemProperties>
<property>
<name>file.encoding</name>
<value>UTF-8</value>
</property>
</systemProperties>
</configuration>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>${version.ojdbc}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>liquibase_01</id>
<properties>
<liquibase.changeLogFile>liquibase_01.xml</liquibase.changeLogFile>
<liquibase.username>${admin.shema.name}</liquibase.username>
<liquibase.password>${admin.shema.password}</liquibase.password>
<liquibase.migrationSqlOutputFile>target/liquibase/migr_01.sql</liquibase.migrationSqlOutputFile>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>${liquibase.execute.goal}</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
我已经尝试了以下方法:
- 在所有 xml 文件中添加了
<?xml...行,但未添加 - 基本上上面所有的 UTF-8 配置都是我新添加的
- 在 maven 命令行中添加了“-Dfile.encoding=UTF-8”
- 将 MAVEN_OPTS 变量设置为“-Dfile.encoding=UTF-8”
我不确定我还能尝试什么。它可能是甲骨文方面的设置吗?字符集似乎是“AL32UTF8”:
select value from nls_database_parameters where parameter='NLS_CHARACTERSET';
AL32UTF8
NLS_LANG 变量未在 Oracle 服务器操作系统上设置。
Oracle 本身安装在具有英文本地化功能的 Windows Server 2008 R2 上。再说一次,具有相同运行时环境的相同 LiquiBase 版本与 SQL Server 2017(安装在具有英文本地化的 Windows Server 2016 上)的情况非常好,使用与上述相同的 INSERT 语句插入重音字符。并且项目的这一部分没有启用上述详细的 UTF-8 设置。
【问题讨论】:
-
你能用notepad++之类的东西检查实际的SQL文件
insert.sql来确定文件的实际编码吗?在更改日志中添加encoding属性<sqlFile>元素实际上不会更改文件的编码,它所做的只是告诉代码尝试使用该编码读取它。如果文件实际上是用 Windows 编码之一编码的,那么当以 UTF-8 格式读取它时,它会被转换为“未知”字符。 -
天啊...我不敢相信我没有注意到。 SQL 文件的编码是 ANSI。我在 Notepad++ 中将其更改为 UTF-8,通过它并修复了损坏的重音字符,现在它也可以正常工作,不需要各种 UTF-8 设置。谢谢!