使用Python将数据从MSSQL数据库复制到Postgresql数据库

发布于 2024-11-14 02:44:00 字数 1928 浏览 2 评论 0原文

我有两个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工作正常。

I have two 2 databases. One with MSSQL and another with Postgresql.
I want that my python script is running (for this I use a cron-job on linux), every day.
The data from the MSSQL database should copied to the Postgresql database. I have an idea but it doesn't works. Could you help me??? Maybe my solution is totally wrong...

That's my code:

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: the request_proc works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事未完 2024-11-21 02:44:00

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

根据文档,还可以启用自动事务。

If the code produces no errors but the inserted records are not appearing in the Postgresql database, you most likely need to add self.conn2.commit() after executing the INSERT statement. This will commit each transaction to the database.

According to the documentation, it is also possible to enable automatic transactions.

百思不得你姐 2024-11-21 02:44:00

您还需要将游标 (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())

you also need to pass the cursor (cur1) into 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())
猫烠⑼条掵仅有一顆心 2024-11-21 02:44:00

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

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

Just seeing this thread after having tried to find the answer myself. You can use a method from psycopg2.extras called execute_values().

In the documentation https://www.psycopg.org/docs/extras.html you can find it near the bottom. Just pass a single string variable, then define that placeholder as a values list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文