根据此文档
Liquibase可以sql在给定路径中执行多个文件,而无需任何更改日志.但是,当我使用以下插入创建文件时
insert into address (id, line1, line2) values (1, '121 Main Ave', null);
insert into address (id, line1, line2) values (2, '662 Broadway', 'Suite 3317');
insert into address (id, line1, line2) values (3, '412 Riverview', null);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
sql语法无效
Cha*_*ant 10
Liquibase无法识别您的sql文件.在你的sql文件上添加这两行:
--liquibase formatted sql
--changeset {authorName}:{id}
Run Code Online (Sandbox Code Playgroud)
根据您的意愿更改authorName和id.您还可以在changelog.xml文件中执行以下操作:
<changeSet author="authorName" id=”id”>
<sqlFile path="insertcommands.sql"/>
</changeSet>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您不需要放在insertcommands.sql文件的顶部
--liquibase formatted sql
--changeset {authorName}:{id}
Run Code Online (Sandbox Code Playgroud)
就像你之前做的那样.
PS - 在liquibase-3.4和mysql5.5上测试
有两种方法可以编写多个查询
1)使用<sqlFile>标签。
path : 所有插入查询写入的文件路径
<changeSet author="liquibase-docs" id="sqlFile-example">
<sqlFile encoding="utf8" path="filepathsql"relativeToChangelogFile="true" splitStatements="true"stripComments="true"/>
</changeSet>`
Run Code Online (Sandbox Code Playgroud)
注意:需要为多个查询编写属性 splitStatements=true 执行
http://www.liquibase.org/documentation/changes/sql_file.html
2)<sql>标签
<changeSet author="liquibase-docs" id="sql-example">
<comment>insert queries</comment>
<sql dbms="h2, oracle"
splitStatements="true"
stripComments="true">
insert into address (id, line1, line2) values (1, "121 Main Ave", null);
insert into address (id, line1, line2) values (2, "662 Broadway", "Suite 3317");
insert into address (id, line1, line2) values (3, "412 Riverview", null);
</sql>
</changeSet>
Run Code Online (Sandbox Code Playgroud)
http://www.liquibase.org/documentation/changes/sql.html
您可以使用 loadData(或 loadUpdate 用于表中的部分更改)来实现以下目的:
<loadData encoding="UTF-8"
file="[path to csv]"
separator=";"
tableName="ADDRESS"/>
Run Code Online (Sandbox Code Playgroud)
并提供包含以下内容的 CSV 文件:
id;line1;line2
1;121 Main Ave;null
2;662 Broadway;Suite 3317
3;412 Riverview;null
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12578 次 |
| 最近记录: |