1

I want to insert some records to mySQL database from PostgreSQL(both are remote servers),So i'm using the below script but the case is that the data is not getting inserted into MySQL database.

There is a final count statement which always results zero.May i know what could be the reason?

Also any improvement need to do on this code.

Suggestions please.

import psycopg2
import os
import time
#import MySQLdb
from mysql.connector import (connection)

import sys
#from pprint import pprint
#from datetime import datetime
#from psycopg2 import sql
#from utils.config import Configuration as Config
#from utils.postgres_helper import get_connection
#from utils.utils import get_global_config

def db_connect():

# MySQLdb connection
 try:
    source_host = 'magento'
    #conf = get_global_config()
    #cnx_msql = MySQLdb.connect(host=conf.get(source_host, 'host'),
                               #user=conf.get(source_host, 'user'),
                               #passwd=conf.get(source_host, 'password'),
                               #port=int(conf.get(source_host, 'port')),
                               #db=conf.get(source_host, 'db'))
    cnx_msql = connection.MySQLConnection(host='server.com', user='db13009',passwd='fgDT***********',port=3306,db='db13009')
    print('MySQL DB connected')
 except mysql.connector.Error as e:
   print ("MYSQL: Unable to connect!", e.msg)
   sys.exit(1)

# Postgresql connection
 try:
   #cnx_psql = get_connection(get_global_config(), 'pg_dwh')
   cnx_psql =psycopg2.connect(host='xxx.xxx.xx.xx',
                            dbname='postgres',
                            port='5432',
                            user='postgres',
                            password='*********')
   print('PSQL DB connected')
 except psycopg2.Error as e:
   print('PSQL: Unable to connect!\n{0}').format(e)
   sys.exit(1)



# Cursors initializations
 cur_msql = cnx_msql.cursor()
 cur_psql = cnx_psql.cursor()
 SQL_test="""SELECT count(*) from action_status;"""
 cur_msql.execute(SQL_test)
 records = cur_msql.fetchall()

 for row in records:
        print("count = ", row[0], )
 msql_command=""
 try:
   SQL_load="""SELECT created_at,payload from staging.sync;"""
   msql_ins="""INSERT INTO action_status(payload,created_at)VALUES (%s, %s) """
   cur_psql.execute(SQL_load)

   for row in cur_psql:
        try:
            print(row[0])
            print(row[1])
            cur_msql.execute(msql_ins, row[0],row[1])
        except psycopg2.Error as e:
            print('ffffffffffffff')
            print ("Cannot execute the query!!", e.pgerror)
            sys.exit(1)
   cnx_msql.commit()

   cur_msql.execute(SQL_test)
   records = cur_msql.fetchall()

   for row in records:
        print("count = ", row[0], )
 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_connect()
8
  • are you getting data from postgresql?? please check it first... and then while inserting each record in mysql, make sure your are commiting to cursor. Commented Oct 23, 2019 at 13:26
  • @AkashPagar Yes, i'm getting data from postgreSQL but it is not inserting to MySQL, I have committed it after the for loop. That is fine right? Commented Oct 23, 2019 at 13:37
  • @AkashPagarDo you see any mistakes or anything can be improved? Commented Oct 23, 2019 at 13:38
  • Did you look into the mysql logs to see, what your query sends to mysql server? Commented Oct 23, 2019 at 16:15
  • @nbk I have no access to that server.But may be i can ask someone to check on that.DO you find any other mistake? Commented Oct 24, 2019 at 7:29

1 Answer 1

1

msql_ins is missing semicolon (not sure whether required or not). More importantly, you are missing a tuple; instead of:

cur_msql.execute(msql_ins, row[0],row[1])

try this instead:

cur_msql.execute(msql_ins, (row[0], row[1]))

Hope that helps :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.