oracle_manage.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # coding=utf-8
  2. '''
  3. Created on Apr 21, 2016
  4. @author: root
  5. '''
  6. import os
  7. import time
  8. os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
  9. import cx_Oracle
  10. from util_common import Constant
  11. class OracleManage(object):
  12. def __init__(self):
  13. self.conn = cx_Oracle.connect(Constant.ORACLE_URL)
  14. def _insert_component(self, component_list, db):
  15. error_component_list = list()
  16. cursor = self.conn.cursor()
  17. cursor.prepare("INSERT INTO product$component_temp ( cmp_id, cmp_code, cmp_img, cmp_uuid, cmp_description, cmp_packaging, cmp_brid, cmp_kiid, cmp_company ) values(:1, :2, :3, :4, :5, :6, :7, :8, :9)")
  18. param_oracle = list()
  19. for index, component in enumerate(component_list):
  20. try:
  21. param_oracle.append( (component["id"], component["code"], component["img"], component["uuid"], component["description"], component["packaging"], component["brandid"], component["kindid"], component["company"]) )
  22. except:
  23. error_component_list.append(component)
  24. # commit data whenever 20000
  25. if index % 20000 == 0:
  26. print ("insert Component :", index)
  27. cursor.executemany(None, param_oracle)
  28. self.conn.commit()
  29. param_oracle.clear()
  30. print (len(error_component_list))
  31. if (len(error_component_list) > 0):
  32. db.insert_into_oracle_failed.insert_many(error_component_list)
  33. cursor.executemany(None, param_oracle)
  34. self.conn.commit()
  35. cursor.close()
  36. def _insert_propertyvalue(self, propertyvalue_list):
  37. cursor = self.conn.cursor()
  38. cursor.prepare('insert into product$propertyvalue_temp (pv_id, pv_componentid, pv_detno, pv_propertyid, pv_value ) values(:1, :2, :3, :4, :5)') #
  39. param_oracle = list()
  40. for index, pv in enumerate(propertyvalue_list):
  41. try:
  42. if len(pv["value"]) > 80:
  43. continue
  44. param_oracle.append( (index + 1, pv["componentid"], pv["detno"], pv["propertyid"], pv["value"]) )
  45. except:
  46. param_oracle.append( (index + 1, pv["componentid"], pv["detno"], pv["propertyid"], None) )
  47. # commit data whenever 50000
  48. if index % 500000 == 0:
  49. print ("insert propertyvalue :", index)
  50. cursor.executemany(None, param_oracle)
  51. self.conn.commit()
  52. param_oracle.clear()
  53. cursor.executemany(None, param_oracle)
  54. self.conn.commit()
  55. cursor.close()
  56. def craw(self, component_list, propertyvalue_list):
  57. conn = cx_Oracle.connect(Constant.ORACLE_URL)
  58. self._insert_component(conn, component_list)
  59. self._insert_propertyvalue(conn, component_list)
  60. conn.close ()
  61. if __name__ == '__main__':
  62. pass