使用Python将数据从一个数据库移植到另一个数据库时出错

时间:2019-05-12 22:19:55

标签: python python-3.x postgresql python-2.7 python-requests

我正面临以下错误,无法继续前进,如果有人对如何解决此问题有任何线索或解决方案,请告诉我。

我正在尝试使用以下逻辑将数据从一个数据库移植到另一个数据库,但被这个错误绊倒了,请为此提供帮助。

  

从PostgreSQL元组索引中获取数据时出错   整数或分片,不是str

import psycopg2
import os
import sys
from pprint import pprint

def psql_fetchandInsert(msql, psql, msql_command, psql_command):
    msql.execute(msql_command)

    for row in msql:
        try:
            psql.execute(psql_command, row)
        except psycopg2.Error as e:
            sys.exit("Some problem occured with the query!!!")



def Fetch():
 try:
   cnx_msql = psycopg2.connect(user="postgres",
                                  password="postgres",
                                  host="127.0.0.1",
                                  port="5432",
                                  database="postgres")
 except psycopg2.Error as e:
   sys.exit(1)

# Postgresql connection
 try:
   cnx_psql = psycopg2.connect(user="postgres",
                                  password="postgres",
                                  host="127.0.0.1",
                                  port="5432",
                                  database="Sendy")
  #cnx_psql = psycopg2.connect(conn_string_psql)
 except psycopg2.Error as e:
   print('PSQL: Unable to connect!\n{0}').format(e)
   sys.exit(1)

 try:
# Cursors initializations
   cur_msql = cnx_msql.cursor()
   cur_psql = cnx_psql.cursor()
   print(" cursor   initialised")
   print("creating table using cursor")

   SQL_create_schema="""CREATE SCHEMA IF NOT EXISTS st AUTHORIZATION postgres;"""

   SQL_create_products="""CREATE TABLE IF NOT EXISTS st.Products
            (     Id        INTEGER
        , Name       CHARACTER VARYING
        , Product      CHARACTER VARYING
        , email             CHARACTER VARYING
        , sku             CHARACTER VARYING
        , html_code   CHARACTER VARYING
        , quote_id        INTEGER
        , itemcount       NUMERIC

        );"""
   cur_psql.execute(SQL_create_schema)


   cur_psql.execute(SQL_create_products)
   cnx_psql.commit();



   commands = [("""SELECT "Id","Name","Product","email","htmlcode","itemcount" from dl."Products";""",
             """INSERT INTO st."Products" (Id,Name,Product,email,htmlcode,itemcount) \
              VALUES (%(Id)s, %(Name)s, %(Product)s, %(email)s, %(html_code)s, %(itemcount)s);""")

            ]
   print ("check")

   for msql_command, psql_command in commands:
     psql_fetchandInsert(cur_msql, cur_psql, msql_command, psql_command)

 except (Exception, psycopg2.Error) as error:
     print ("Error while fetching data from PostgreSQL", error)
 finally:
     ## Closing cursors
     cur_msql.close()
     cur_psql.close()
     ## Committing
     cnx_psql.commit()
     ## Closing database connections
     cnx_msql.close()
     cnx_psql.close()


if __name__ == '__main__':
    Fetch()

0 个答案:

没有答案