【问题标题】:copy data from MSSQL database to Postgresql database with Python使用 Python 将数据从 MSSQL 数据库复制到 Postgresql 数据库
【发布时间】:2011-06-07 11:46:24
【问题描述】:

我有两个 2 数据库。一个使用 MSSQL,另一个使用 Postgresql。 我希望我的 python 脚本每天都在运行(为此我在 linux 上使用 cron-job)。 MSSQL 数据库中的数据应复制到 Postgresql 数据库。我有一个想法,但它不起作用。你可以帮帮我吗???也许我的解决方案完全错误......

这是我的代码:

import pymssql, psycopg2

class DatabaseRequest:

    def __init__(self):
        self.conn1 = pymssql.connect(host='****', user='****', password='****', database='****')
        self.conn2 = psycopg2.connect("dbname='****' user='****' host='*****' password='****'")

        self.cur1 = self.conn1.cursor()
        self.cur2 = self.conn2.cursor()

    def request_proc(self, rows):
        self.cur1.execute("SELECT top 10  tag, site, plant, unit, line, ProcessID AS pid, Count(ReadTime) AS mods \
                        FROM ( \
                        select dateadd(dd, -1, convert(varchar, getDate(),111)) \
                        as tag, ReadTime, processID, subid, PR.Site, PR.Plant, PR.Unit, PR.Line \
                        from FactBarcodeReading BCR with(nolock) \
                        inner join DimProcess PR on BCR.ProcessKey = PR.ProcessKey \
                        where PR.ProcessID IN  (802, 1190, 1800, 3090, 3590, 4390, 4590, 4800, 5000, 5400, 4190) \
                        and ReadTime between dateadd(dd, -1, convert(varchar, getDate(),111)) and dateadd(dd, -0, convert(varchar, getDate(),111)) \
                        ) a \
                        GROUP BY tag, site, plant, unit, line, ProcessID \
                        ORDER BY site, plant, unit, line, ProcessID")

        rows = self.cur1.fetchone()

       # return rows   


    def insert_proc(self):
        self.cur2.execute("INSERT INTO 20091229global (proddate, site, plant, unit, line, pid, mods) \
                        VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')"
                        % self.cur1.fetchone())


        a = DatabaseRequest()
        print a.insert_proc()

PS:request_proc 工作正常。

【问题讨论】:

    标签: python psycopg2 pymssql


    【解决方案1】:

    如果代码没有产生错误但插入的记录没有出现在 Postgresql 数据库中,您很可能需要在执行 INSERT 语句后添加self.conn2.commit()。这会将每个事务提交到数据库。

    根据documentation,也可以开启自动交易。

    【讨论】:

      【解决方案2】:

      你还需要将光标 (cur1) 传递给 insert_proc -->

      def insert_proc(self, cur1):
        self.cur2.execute("INSERT INTO 20091229global (proddate, site, plant, unit, line, pid, mods) \
                              VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')"
                              % self.cur1.fetchone())
      

      【讨论】:

        【解决方案3】:

        在自己尝试找到答案后才看到这个帖子。您可以使用 psycopg2.extras 中称为 execute_values() 的方法。

        在文档https://www.psycopg.org/docs/extras.html 中,您可以在底部附近找到它。只需传递一个字符串变量,然后将该占位符定义为值列表。

        【讨论】:

          猜你喜欢
          • 2012-01-19
          • 2017-06-03
          • 1970-01-01
          • 2016-07-28
          • 2010-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-30
          相关资源
          最近更新 更多