PostgreSQL元组索引必须是整数或切片,而不是str

时间:2019-05-10 19:55:19

标签: python python-3.x

在下面的代码中遇到上述错误,有人可以指导我吗?

import psycopg2
import os
import time
import MySQLdb
import sys
from pprint import pprint
from datetime import datetime
from psycopg2 import sql


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

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


def dB_Fetch():

# MySQLdb connection
 try:
    source_host = 'magento'
    conf = get_global_config()
    cnx_msql = psycopg2.connect(user="postgres",
                              password="postgres",
                              host="127.0.0.1",
                              port="5432",
                              database="clone")
except mysql.connector.Error as e:
   print "MYSQL: Unable to connect!", e.msg
   sys.exit(1)

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

 # Cursors initializations
  cur_msql = cnx_msql.cursor(dictionary=True)
  cur_psql = cnx_psql.cursor()

  try:
    print("creating table using cursor")
   SQL_create_Staging_schema="""CREATE SCHEMA IF NOT 
   EXISTS staging AUTHORIZATION postgres;"""

 SQL_create_sales_flat_quote="""CREATE TABLE IF NOT EXISTS 
staging.sales_flat_quote
        (
              customer_id         BIGINT
            , entity_id           BIGINT
    , store_id        BIGINT
    , customer_email      TEXT
    , customer_firstname  TEXT
    , customer_middlename TEXT
    , customer_lastname   TEXT
    , customer_is_guest   BIGINT
    , customer_group_id   BIGINT
    , created_at          TIMESTAMP WITHOUT TIME ZONE
    , updated_at          TIMESTAMP WITHOUT TIME ZONE
    , is_active             BIGINT
    , items_count           BIGINT
    , items_qty             BIGINT
    , base_currency_code            TEXT
    , grand_total           NUMERIC(12,4)
    , base_to_global_rate           NUMERIC(12,4)
    , base_subtotal         NUMERIC(12,4)
    , base_subtotal_with_discount   NUMERIC(12,4)
    )
  ;"""

 SQL_create_sales_flat_quote_item="""CREATE TABLE IF NOT 
  EXISTS staging.sales_flat_quote_item
        (    store_id         INTEGER
    , row_total       NUMERIC
    , updated_at      TIMESTAMP WITHOUT TIME ZONE
    , qty             NUMERIC
    , sku             CHARACTER VARYING
    , free_shipping   INTEGER
    , quote_id        INTEGER
    , price       NUMERIC
    , no_discount     INTEGER
    , item_id     INTEGER
    , product_type    CHARACTER VARYING
    , base_tax_amount NUMERIC
    , product_id      INTEGER
    , name        CHARACTER VARYING
    , created_at      TIMESTAMP WITHOUT TIME ZONE
    );"""

  print("Creating  Schema")   
  cur_psql.execute(SQL_create_Staging_schema)
  print("schema  succesfully created")

 print("Creating staging.sales_flat_quote table")
 cur_psql.execute(SQL_create_sales_flat_quote)
 print("staging.sales_flat_quote table  succesfully created")

  print("Creating staging.sales_flat_quote_item table")
 cur_psql.execute(SQL_create_sales_flat_quote_item)
  print("staging.sales_flat_quote_item table  succesfully 
 created")
  cnx_psql.commit();

  print("Fetching data from source server")
  #select from source & insert into destination SQL statement is 
 created as PAIRS for looping.
 commands = [
          ("SELECT customer_id, entity_id, store_id, customer_email 
 , customer_firstname, customer_middlename, 
 customer_lastname , customer_is_guest, customer_group_id, 
 created_at, updated_at, is_active, items_count, items_qty, 
 base_currency_code, grand_total, base_to_global_rate, 
  base_subtotal, base_subtotal_with_discount from 
 sales_flat_quote where is_active=1 AND items_count != '0' AND 
 updated_at > '2019-05-09 00:00:00';",
         "INSERT INTO staging.sales_flat_quote (customer_id, 
  entity_id, store_id, customer_email , customer_firstname, 
   customer_middlename, customer_lastname , 
   customer_is_guest, customer_group_id, created_at, 
    updated_at, is_active, items_count, items_qty, 
   base_currency_code, grand_total, base_to_global_rate, 
   base_subtotal, base_subtotal_with_discount) \
          VALUES (%(customer_id)s, %(entity_id)s, %(store_id)s,%. 
    (customer_email)s,%(customer_firstname)s,%. 
   (customer_firstname)s,%(customer_middlename)s,%. 
  (customer_lastname)s,%(customer_is_guest)s, %. 
  (customer_group_id)s, %(created_at)s, %(updated_at)s, %. 
  (is_active)s, %(items_count)s, %(items_qty)s, %. 
  (base_currency_code)s, %(grand_total)s, %. 
  (base_to_global_rate)s, %(base_subtotal)s, %. 
  (base_subtotal_with_discount)s)"),

        ("SELECT  

store_id,row_total,updated_at,qty,sku,免运费,quote_id,价格,无折扣,item_id,product_type,base_tax_amount,product_id,名称,sales_flat_quote_item的创建日期,“              “将ININSERT INTO暂存。 ,%(updated_at)s,%(qty)s,%(sku)s,%(free_shipping)s,%(quote_id)s,%(price)s,%(no_discount)s,%(item_id)s,% (product_type)s,%(base_tax_amount)s,%(product_id)s,%(name)s,%(created_at)s)“)

     ,("SELECT  

created_at,url_path,price,short_description,url_key,thumbnail_label。 ,小图片,缩略图,名称,sku,来自的type_id       catalog_product_flat_1”,              “将INERT插入staging.catalog_product_flat_1

(created_at,url_path,price,short_description,url_key,thumbnail_lab      el,small_image,thumbnail,name,sku,type_id)\               VALUES(%(created_at)s,%(url_path)s,%(price)s,%。        (short_description)s,%(url_key)s,%(thumbnail_label)s,%。        (small_image)s,%(thumbnail)s,%(name)s,%(sku)s,%。        (type_id)s)“)             ]

   for msql_command, psql_command in commands:
       psql_Source_fetch_and_DestInsert(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__':
    dB_Fetch()

0 个答案:

没有答案