gpt4 book ai didi

mysql - DDLUtils不使用ant脚本导出Mysql数据

转载 作者:行者123 更新时间:2023-11-29 13:46:11 25 4
gpt4 key购买 nike

我想使用ant中的ddlutils工具导出mysql数据库

 <target name="export-source-db" description="Dumps db structure and data">
<taskdef name="databaseToDdl"
classname="org.apache.ddlutils.task.DatabaseToDdlTask">
<classpath refid="libraries"/>
<classpath refid="mysqlclasspath"/>
</taskdef>

<databaseToDdl modelName="bwfla">
<database url="jdbc:mysql://localhost:3306/"
driverClassName="com.mysql.jdbc.Driver"
username="root"
password="sriram"/>
<writeSchemaToFile outputFile="db-schema.xml"/>
<writeDataToFile outputFile="data.xml"/>
</databaseToDdl>

</target>

但是如果我检查 db-schema.xml

<?xml version="1.0"?>
<!DOCTYPE database SYSTEM "http://db.apache.org/torque/dtd/database">
<database name="bwfla"/>

data.xml 是

<?xml version='1.0' encoding='UTF-8'?>
<data>
</data>

它没有导出数据。谁能帮助我。

最佳答案

我认为你的问题是数据库 URL:

jdbc:mysql://localhost:3306/

您尚未指定要连接的数据库。应该是这样的:

jdbc:mysql://localhost:3306/my_db_name_goes_here

我还建议不要以“root”身份连接。创建一个有权访问数据库的 mysql 用户。

旁白:结账liquibase 。我认为它是管理数据库模式的更强大的工具。

工作示例

我使用apache ivy管理我的第 3 方依赖项。只需忽略“引导”和“解析”目标即可。

<project name="ddutils" default="create" xmlns:ivy="antlib:org.apache.ivy.ant">

<property name="db.driver" value="com.mysql.jdbc.Driver"/>
<property name="db.url" value="jdbc:mysql://localhost:3306/example1"/>
<property name="db.username" value="example1"/>
<property name="db.password" value="pleasechangeme"/>

<target name="bootstrap" description="Install ivy">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
</target>

<target name="resolve" description="Resolve 3rd party dependencies">
<ivy:cachepath pathid="build.path">
<!-- Database -->
<dependency org="mysql" name="mysql-connector-java" rev="5.1.25" conf="default"/>

<!-- ddlutils plus dependency fixes -->
<dependency org="org.apache.ddlutils" name="ddlutils" rev="1.0" conf="default"/>
<dependency org="xml-apis" name="xml-apis" rev="1.0.b2" conf="default" force="true"/>
<dependency org="xerces" name="xercesImpl" rev="2.11.0" conf="default"/>
<exclude org="xerces" module="xerces"/>

<!-- logging libraries -->
<dependency org="org.slf4j" name="slf4j-simple" rev="1.7.5" conf="default"/>
<dependency org="org.slf4j" name="log4j-over-slf4j" rev="1.7.5" conf="default"/>
</ivy:cachepath>
</target>

<target name="create" depends="resolve" description="Create tables and data">
<sql driver="${db.driver}" url="${db.url}" userid="${db.username}" password="${db.password}" classpathref="build.path">
CREATE TABLE example1 (id INT, one VARCHAR(30), two VARCHAR(30), PRIMARY KEY(id));
INSERT INTO example1 VALUES (0, 'hello', 'world');
INSERT INTO example1 VALUES (1, 'hello', 'world');
INSERT INTO example1 VALUES (2, 'hello', 'world');
INSERT INTO example1 VALUES (3, 'hello', 'world');
INSERT INTO example1 VALUES (4, 'hello', 'world');
INSERT INTO example1 VALUES (5, 'hello', 'world');
INSERT INTO example1 VALUES (6, 'hello', 'world');
INSERT INTO example1 VALUES (7, 'hello', 'world');
INSERT INTO example1 VALUES (8, 'hello', 'world');
INSERT INTO example1 VALUES (9, 'hello', 'world');

CREATE TABLE example2 (id INT, one VARCHAR(30), two VARCHAR(30), PRIMARY KEY(id));
INSERT INTO example2 VALUES (0, 'hello', 'world');
INSERT INTO example2 VALUES (1, 'hello', 'world');
INSERT INTO example2 VALUES (2, 'hello', 'world');
INSERT INTO example2 VALUES (3, 'hello', 'world');
INSERT INTO example2 VALUES (4, 'hello', 'world');
INSERT INTO example2 VALUES (5, 'hello', 'world');
INSERT INTO example2 VALUES (6, 'hello', 'world');
INSERT INTO example2 VALUES (7, 'hello', 'world');
INSERT INTO example2 VALUES (8, 'hello', 'world');
INSERT INTO example2 VALUES (9, 'hello', 'world');
</sql>
</target>

<target name="extract" depends="resolve" description="Use DDLUtils to extract schema and data">
<taskdef classname="org.apache.ddlutils.task.DatabaseToDdlTask" name="databaseToDdl" classpathref="build.path" />

<databaseToDdl usedelimitedsqlidentifiers="true" modelname="example">
<database driverclassname="${db.driver}" url="${db.url}" username="${db.username}" password="${db.password}"/>
<writeschematofile outputfile="build/schema.xml"/>
<writedatatofile outputfile="build/data.xml" encoding="ISO-8859-1"/>
</databaseToDdl>
</target>

<target name="clean" description="Cleanup project files">
<delete dir="build"/>
</target>

<target name="clean-all" depends="clean" description="Cleanup project files">
<ivy:cleancache/>
</target>

</project>

关于mysql - DDLUtils不使用ant脚本导出Mysql数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17385652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com