Advertisement
Guest User

Untitled

a guest
Oct 9th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. # First we need to import all the modules we will be using - for now its only 3 easy ones
  2. import string, pymssql, cx_Oracle
  3.  
  4. ## Define Oracle connection - format ("username/password@TNSNAME")
  5. ora_conn = cx_Oracle.connect('warrantytracker', 'warranty#tracker', '10.2.2.50:1521/dwhdev')
  6.  
  7. ## Define the Oracle cursor objects
  8. ora_cursor = ora_conn.cursor() #Allocate a cursor to that particular database connection
  9.  
  10. ## Define the MSSQL server connection - format is self-explanitory
  11. mssql_conn = pymssql.connect(host='10.4.14.25', user='sa', password='sa', database='warranty')
  12.  
  13. ## Define the MSSQL cursor objects
  14. mssql_cursor = mssql_conn.cursor() #Allocate a cursor to that particular database connection
  15.  
  16.  
  17. ## Truncate our destination tables
  18. ora_cursor.execute("truncate table test")
  19. mssql_cursor.execute("truncate table test")
  20.  
  21.  
  22.  
  23. ## Fetch our source rows into a cursor
  24. ## (this is just using a simple function to randomly generate a bunch of garbage strings for now)
  25. ora_cursor.execute("""SELECT DBMS_RANDOM.STRING('P',40) field1,
  26. DBMS_RANDOM.STRING('X',30) field2, ROUND(DBMS_RANDOM.VALUE(1000, 9999)) field3,
  27. DBMS_RANDOM.STRING('A',20) field4 FROM DUAL CONNECT BY LEVEL<=3000""")
  28.  
  29. ResultSet_Py_List = [] #Create an empty list, then and populate it with cursor results (below)
  30. # Basically we're looping through the now-loaded cursor, pulling out the results and appending them into a Python "List" object
  31. # Why? Well because this makes the inserts MUCH easier to deal with AND if we needed to do any data transformation - its easier.
  32. for field1, field2, field3, field4 in ora_cursor:
  33. try:
  34. ResultSet_Py_List.append((field1, field2, field3, field4))
  35. except AttributeError: #if we run out of rows - which we will evevntually, we don't want the interpreter to freak out
  36. pass
  37.  
  38. # just do a quick count of the list we created to make sure that we've got all our rows
  39. print str(len(ResultSet_Py_List)) + ' Records SELECTED from Source (into list ready for insertion)'
  40.  
  41. # For the Oracle insert we need to do a "prepare" with the paramatized SQL and then an "executemany" with the actual list
  42. # Notice we're re-using the cursor from before since we don't need it anymore - all the values have be put in the ResultSet_Py_List list object
  43. ora_cursor.prepare("""INSERT INTO test (field1, field2, field3, field4) VALUES (:field1, :field2, :field3, :field4)""")
  44. ora_cursor.executemany(None, ResultSet_Py_List)
  45. ora_conn.commit() #COMMIT that shit before that data gets away!
  46.  
  47.  
  48. #insert statement with bind variables
  49. mssql_cursor.executemany("INSERT INTO test (field1, field2, field3, field4) VALUES (%s, %s, %s, %s)", ResultSet_Py_List)
  50. mssql_conn.commit() #COMMIT that shit before that data gets away!
  51.  
  52.  
  53.  
  54. #ora_cursor.execute("select count(*) from test")
  55. #for row in ora_cursor:
  56. # print str(row).replace("(","").replace(")","").replace(","," ") + 'Records INSERTED into Oracle TEST table'
  57.  
  58. ora_cursor.execute("select count(*) from test")
  59. ora_row_count = ora_cursor.fetchone()
  60. print str(ora_row_count[0]) + ' Records INSERTED into Oracle TEST table'
  61.  
  62. mssql_cursor.execute("select count(*) from rrobitaille.test")
  63. mssql_row_count = mssql_cursor.fetchone()
  64. print str(mssql_row_count[0]) + ' Records INSERTED into MSSQL TEST table'
  65.  
  66. ## All done, Lars. Now lets get a drink.
  67.  
  68. ## We will even be nice and close the connections.
  69. ## Its like tipping your hat to the Database engine.
  70. mssql_conn.close()
  71. ora_conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement