{"id":1032,"date":"2011-05-31T18:55:49","date_gmt":"2011-05-31T23:55:49","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=1032"},"modified":"2016-07-25T09:35:16","modified_gmt":"2016-07-25T14:35:16","slug":"python-row-copier","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2011\/05\/31\/python-row-copier\/","title":{"rendered":"Python row copier"},"content":{"rendered":"<p>Below is a script I wrote to copy a set of tables from one schema (or database) to another.  <\/p>\n<p>It simply takes every table in the source schema and builds up an array based copy for it, with the usage of bind variables and bulk inserting.  It assumes the target table structures have already been built, but without data.  It also doesn&#8217;t handle constraint violations, although that shouldn&#8217;t be too difficult to implement.<\/p>\n<pre lang=\"python\" line=\"1\">\r\nimport cx_Oracle, sys\r\n\r\n#-------------------------------------------------------------------------------------------\r\n\"\"\"\r\n#Author:\tSteve Howard\r\n#Date:\t\tJanuary 30, 2011\r\n#Purpose:\tSimple array based row copier.  It works best for smaller row sets (less\r\n\t\tthan one million rows per table), as it reads an entire resultset into\r\n\t\tmemory, inserts it into the target table, then commits and releases the\r\n\t\tmemory.\r\n\t\t\r\n\t\tA possible enhancement to this design is to periodically perform a bulk\r\n\t\tinsert into the target once the scan of the source table gets to a\r\n\t\tspecified number of rows or size, and then clear the array and start\r\n\t\treloading again.  This shouldn't be too difficult to implement, actually.\r\n\t\t\r\n\t\tAdded what is above, so it should scale better.\r\n\"\"\"\r\n#------------------------------------------------------------------------------------------\r\n\r\nsource = cx_Oracle.connect(\"rep\",\"rep\")\r\nselect = source.cursor()\r\nthistable = source.cursor()\r\n\r\nselect.execute(\"select table_name from user_tables\")\r\ntarget = cx_Oracle.connect(\"rep\",\"rep\")\r\ninsert = target.cursor()\r\n\r\nfound = False\r\nthisrow=[]\r\nfor table in select:\r\n  rows = []\r\n  s = \"\"\r\n  i = -1\r\n  try:\r\n    thistable.execute(\"select * from \" + table[0])\r\n    colcount = len(thistable.description)\r\n    for col in range(colcount):\r\n      i = i + 1\r\n      s = s + \":b\" + str(i) + \",\"\r\n    s = s[0:len(s) - 1]\r\n    found = False\r\n    j = 0\r\n    for row in thistable:\r\n      j = j + 1\r\n      # we have at least one row to copy\r\n      found = True\r\n      for col in range(colcount):\r\n        thisrow.append(row[col])\r\n      rows.append(thisrow)\r\n      thisrow=[]\r\n      if j % 1000 == 0:\r\n        insert.prepare(\"insert into \" + table[0] + \" values(\" + s + \")\")\r\n        insert.executemany(None, rows)\r\n        target.commit()\r\n        rows = []\r\n\r\n  except:\r\n    print str(sys.exc_info()[1])\r\n  #only copy if we actually found data\r\n  if found == True:\r\n    insert.prepare(\"insert into \" + table[0] + \" values(\" + s + \")\")\r\n    insert.executemany(None, rows)\r\n    target.commit()\r\n\r\nselect.close()\r\nsource.close()\r\ntarget.close()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below is a script I wrote to copy a set of tables from one schema (or database) to another. It simply takes every table in the source schema and builds up an array based copy for it, with the usage&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2011\/05\/31\/python-row-copier\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[19,24,22,26],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1032"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1032"}],"version-history":[{"count":10,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1032\/revisions"}],"predecessor-version":[{"id":1281,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/1032\/revisions\/1281"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1032"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}