Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. ini_set('memory_limit', '60000M');
  2.  
  3. $path = '/var/www/html/csv/';
  4.  
  5. $files = array('ACELERA2.csv','ATLAS.csv','BOLSA.csv','CADASTRO_DMA.csv','CADASTRO_INFRA.csv','CAPITAL.csv','CHEGAMAIS.csv','CPMF.csv','ECOOL.csv','ENERGIA.csv','ENQUADRAMENTO.csv','INOVACAO.csv','INTELIGENCIA.csv','JUROS.csv','MEU_NOVO_MUNDO.csv','MONITOR.csv','NEXCODE.csv','PESQUISAEXPORTACAO.csv','PREMIO_AGUA.csv','PREMIO_MERITO.csv','PROTHEUS.csv','RM.csv','RODADAS.csv','SCE.csv','SCN.csv','SCS.csv','SGC.csv','SINDI.csv','SMS.csv','SPVNE.csv','SR.csv' );
  6.  
  7. $insert = null;
  8.  
  9. foreach ($files as $file )
  10. {
  11. $rrow = null;
  12.  
  13.  
  14.  
  15. $fp = fopen($path.$file, "r");
  16.  
  17. if ($fp)
  18. {
  19. $head = fgetcsv($fp, 0, ";", '"');
  20.  
  21. $count = count($head);
  22.  
  23. $collun = '(';
  24.  
  25. //$collun ='CREATE TABLE '.substr($file,0,-4).' ('."rn";
  26.  
  27. for ($i=0; $i < 22; $i++) {
  28.  
  29. if ( $i !== $count )
  30. {
  31. $collun.= $head[$i].',';
  32. }
  33. }
  34.  
  35. $collun_ = substr($collun,0,-1).')';
  36.  
  37. while ( $data = fgetcsv($fp, 0, ";",'"') )
  38. {
  39.  
  40. $row = '(';
  41.  
  42. for ($i=0; $i < 22; $i++)
  43. {
  44. if ( $data[$i] == null )
  45. {
  46. $insert = 'null';
  47. }
  48. else
  49. {
  50. $insert = $data[$i];
  51. }
  52. $row.= "$$".$insert."$$,";
  53. }
  54.  
  55. $rrow.= substr($row,0,-1).')';
  56.  
  57. $sql = 'INSERT INTO pf '.$collun_.' values '.$rrow.' ON CONFLICT (cpf) DO NOTHING';
  58.  
  59. echo $sql;
  60.  
  61. sleep(3);
  62.  
  63. $dbconn = pg_connect("host=localhost dbname=xxx user=xxxx password=xxxx")
  64. or die('Could not connect: ' . pg_last_error());
  65.  
  66. $result = pg_query($dbconn, $sql);
  67.  
  68. var_dump($result);
  69.  
  70. pg_close($dbconn);
  71.  
  72. }
  73.  
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement