testInsertcomponent.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #coding=utf-8
  2. '''
  3. Created on 2016年6月7日
  4. @author: uas
  5. '''
  6. import os
  7. import time
  8. os.environ['NLS_LANG']='SIMPLIFIED CHINESE_CHINA.UTF8'
  9. from pymongo.mongo_client import MongoClient
  10. import cx_Oracle
  11. from util_common import Constant
  12. class OracleManager(object):
  13. def __init__(self):
  14. self.conn=cx_Oracle.connect(Constant.ORACLE_URL)
  15. def _insert_component_temp(self,component_list):
  16. cursor=self.conn.cursor()
  17. # sql="create table product$component_temp1 (cmp_id int primary key,cmp_attach varchar(255) ,cmp_img varchar(255),cmp_brid int ,cmp_code varchar(255),cmp_kiid int,cmp_company varchar(255) ,cmp_company_url varchar(255) ,cmp_description varchar(4000),cmp_packaging varchar(255))"
  18. sql="drop table PRODUCT$COMPONENT_TEMP1"
  19. cursor.execute(sql)
  20. self.conn.commit()
  21. # print(self.conn.version)
  22. # cursor.prepare("INSERT INTO product$component_temp1 (cmp_id,cmp_attach,cmp_img,cmp_brid,cmp_code,cmp_kiid,cmp_company,cmp_company_url,cmp_description,cmp_packaging) values(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10)")
  23. # param_oracle=list()
  24. # error_component_list=list()
  25. # for index,component in enumerate(component_list):
  26. # try:
  27. # param_oracle.append((component['cmp_id'],component['cmp_attach'],component['cmp_img'],component['cmp_brid'],component['cmp_code'],component['cmp_kiid'],component['cmp_company'],component['cmp_company_url'],component['description'],component['cmp_packaging']))
  28. # except Exception as e:
  29. # error_component_list.append(component)
  30. #
  31. #
  32. # if index %20000 ==0:
  33. #
  34. # cursor.executemany(None,param_oracle)
  35. #
  36. # self.conn.commit()
  37. # param_oracle.clear()
  38. # print(index)
  39. # if index>10000:
  40. # break
  41. # print( len(error_component_list))
  42. # print(2)
  43. # print(param_oracle)
  44. # cursor.executemany(None,param_oracle)
  45. # print(1)
  46. # self.conn.commit()
  47. # print(3)
  48. # cursor.close()
  49. #
  50. def craw(self,component_list,propertyvalue_list):
  51. self._insert_component_temp(component_list)
  52. # self._insert_property_value(propertyvalue_list)
  53. #
  54. # def _insert_property_value(self,propertyvalue_list):
  55. # cursor=self.conn.cursor()
  56. # cursor.prepare('insert into product$propertyvalue_temp (pv_id,pc_componentid,pv_detno,pv_propertyid,pv_value) values(:1,:2,:3,:4,:5)')
  57. # param_oracle=list()
  58. # for index,pv in enumerate(propertyvalue_list):
  59. # try:
  60. # if len(pv["value"])>80:
  61. # continue
  62. # param_oracle.append((index+1,pv['cmp_id'],pv['detno']-1,pv['propertyid'],pv['value']))
  63. # except:
  64. # param_oracle.append((index+1,pv['cmp_id'],pv['detno']-1,pv['propertyid'],None))
  65. # if index%50000==0:
  66. # cursor.executemany(None,param_oracle)
  67. # param_oracle.clear()
  68. #
  69. # cursor.executemany(param_oracle)
  70. # self.conn.commit()
  71. # cursor.close()
  72. if __name__=='__main__':
  73. Oracle=OracleManager()
  74. cli=MongoClient(Constant.MONGODB_URL)
  75. db=cli.spider
  76. component_list=db.component_temp_0607.find().limit(100)
  77. component_list_temp=list()
  78. for row in component_list:
  79. component_list_temp.append(row)
  80. print(row)
  81. # propertyvalue_list=db.propertyvalue_0607.find()
  82. Oracle.craw(component_list_temp,None)