首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scriptella:将表从access复制到postgresql

Scriptella:将表从access复制到postgresql
EN

Stack Overflow用户
提问于 2013-01-24 02:06:52
回答 1查看 1.1K关注 0票数 0

我是Scriptella新手,我完全被以下问题困扰了。

我已经给出了访问表。我正在使用我编写的程序获取表模式。输出文件(tableschema.xml)如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<table>
    <column>ID</column>
    <datatype>COUNTER</datatype>
    <column>FirstName</column>
    <datatype>VARCHAR</datatype>
    <column>LastName</column>
    <datatype>VARCHAR</datatype>
    <column>Salary</column>
    <datatype>CURRENCY</datatype>
    <column>SSN</column>
    <datatype>INTEGER</datatype>
</table>

然后,使用脚本,我需要创建一个新的PostgreSQL数据库(如果可能的话。如果不可能,我们可以假设数据库已经创建)。然后我需要使用提供的XML文件创建一个新的表(需要),并将所有数据从access表复制到PostgreSQL表。

我实现了从access中提取数据。我实现了从XML文件中提取信息。

我遇到了在Postgres中创建DB和表的问题。看起来CREATE DATABASECREATE TABLE并不能通过脚本来工作。

我的etl.xml文件的粗略草稿:

代码语言:javascript
复制
<!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd">
<etl>
    <connection id="db1" url="jdbc:odbc:TestDB" driver="scriptella.driver.odbc.Driver" user="Dzmitry" password="a"/>

    <connection id="db2" url="jdbc:odbc:PostgreSQLconnector" driver="scriptella.driver.odbc.Driver" classpath="postgresql.jar" user="Dzmitry" password="a"/>

    <connection id="script" driver="script"/>
    <connection id="java" driver="scriptella.driver.janino.Driver"/>
    <connection id="log" driver="text"/>

    <!--  doesn't work

    <script connection-id="db2" new-tx="true" if="create_databases">
            CREATE DATABASE testDB;
    </script>


    -->


    <query connection-id="db1">
        SELECT * FROM GenInfo;

        <script connection-id="script">
            java.lang.System.out.println("Processing row number " + rownum + " ");
        </script>
        <script connection-id="log">
            ${FirstName}, ${LastName}, ${Salary}, ${SSN}
        </script>
    </query>

    <query connection-id="java">
        import javax.xml.parsers.*;
        import javax.xml.xpath.*;
        import java.io.*;
        import org.w3c.dom.Document;
        import org.xml.sax.*;

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();

        File file = new File("tableschema.xml");
        Document document = docBuilder.parse(file);

        XPathFactory xpFactory = XPathFactory.newInstance();
        XPath xPath = xpFactory.newXPath();

        int numberOfColumns = Integer.parseInt(xPath.evaluate("count(/table/column)", document));


        for(int i = 1; i &lt;= numberOfColumns; i++){ // does not take less than sign
            System.out.print("Column name " + i + ": " + xPath.evaluate("/table/column[" + i + "]", document));
            System.out.println("\tData type " + i + ": " + xPath.evaluate("/table/datatype[" + i + "]", document));
        }

    </query>
</etl>

有人能帮我吗?

问候你,兹米特里。

EN

回答 1

Stack Overflow用户

发布于 2013-01-25 17:28:38

在您的脚本中,您使用ODBC驱动程序来访问Postgres:

代码语言:javascript
复制
driver="scriptella.driver.odbc.Driver"

我建议使用document中解释的JDBC驱动程序:

代码语言:javascript
复制
<connection driver="postgresql" url="jdbc:postgresql://localhost:5432/DATABASENAME" user="username" password="password">
</connection>

此外,您可以省略new-tx="true“属性,我认为在您的场景中没有必要使用它。因此,从下面这样简单的事情开始:

代码语言:javascript
复制
<connection connection-id="db2" driver="postgresql" url="jdbc:postgresql://localhost:5432/DATABASENAME" user="Dzmitry" password="a" classpath="postgresql.jar">
</connection>

<script connection-id="db2">
    CREATE TABLE distributors (
        did     integer PRIMARY KEY,
        name    varchar(40)
    );
</script>

在本例中,我展示了如何为现有数据库DATABASENAME创建表。如果您想创建另一个数据库,脚本将更加复杂,但在我提供更多详细信息之前,我需要您确认您现在可以在现有数据库中创建表。

为了创建一个数据库,首先你必须连接到一个像template1这样的现有数据库(它应该是预装的)。并从那里创建另一个数据库。然后使用单独的连接执行其他脚本。请注意,create table脚本的连接必须启用lazy-init属性,否则将对不存在的数据库抛出错误:

代码语言:javascript
复制
<!-- Connection for just for creating a database -->
<connection connection-id="db2_init" driver="postgresql" url="jdbc:postgresql://localhost:5432/template1" user="postgres" password="postgres" classpath="postgresql.jar">
</connection>

<!-- Connection for DDL and data statements. Needs to be lazy-init=true -->
<connection connection-id="db2_tables" driver="postgresql" lazy-init="true" url="jdbc:postgresql://localhost:5432/testDB" user="Dzmitry" password="a" classpath="postgresql.jar">
</connection>

<script connection-id="db2_init" if="create_databases">
    CREATE DATABASE testDB;
</script>
<script connection-id="db2_tables" if="create_schema">
    CREATE TABLE distributors (
        did     integer PRIMARY KEY,
        name    varchar(40)
    );
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14486400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档