-
Notifications
You must be signed in to change notification settings - Fork 14
/
import.php
172 lines (140 loc) · 3.95 KB
/
import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
require_once('bar.php');
$mysqli = new mysqli('localhost', 'ctt', 'dm9WYUEC64bEbFv6', 'CTT');
$dbName = 'CTT';
echo date('Y-m-d H:i:s')."|Importing Distritos...\n";
var_dump(importDistritos('external/distritos.txt', 'CTT', 'distritos', $mysqli));
echo date('Y-m-d H:i:s')."|Importing Concelhos...\n";
var_dump(importConcelhos('external/concelhos.txt', 'CTT', 'concelhos', $mysqli));
echo date('Y-m-d H:i:s')."|Importing post codes...\n";
var_dump(importCodigosPostais('external/todos_cp.txt', 'CTT', 'codigosPostais', $mysqli));
echo date('Y-m-d H:i:s')."|Done!\n";
function importDistritos($fileName, $dbName, $tableName, $mysqli){
$distritos = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$insQry = "INSERT INTO $dbName.$tableName (Codigo_Distrito, Designacao_Distrito) VALUES\n";
foreach($distritos as $line){
$fields = explode(';', $line);
$cod = $fields[0];
$desig = $fields[1];
$insQry .= "('$cod', '$desig'),\n";
}
// remove last ,\n
$insQry = substr($insQry,0,-2);
#var_dump($insQry);
$qid = $mysqli->query($insQry);
if(!$qid){
var_dump($mysqli->error);
}else{
var_dump($qid);
}
}
function importConcelhos($fileName, $dbName, $tableName, $mysqli){
$concelhos = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$insQry = "INSERT INTO $dbName.$tableName (Codigo_Distrito, Codigo_Concelho, Designacao_Concelho) VALUES\n";
foreach($concelhos as $line){
$fields = explode(';', $line);
$cod_d = $fields[0];
$cod_c = $fields[1];
$desig = $fields[2];
$insQry .= "('$cod_d', '$cod_c', '$desig'),\n";
}
// remove last ,\n
$insQry = substr($insQry,0,-2);
// Insert
$qid = $mysqli->query($insQry);
if(!$qid){
var_dump($mysqli->error);
}else{
var_dump($qid);
}
}
function importCodigosPostais($fileName, $dbName, $tableName, $mysqli){
$codigosPostais = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$total = count($codigosPostais);
$insQry = "
INSERT INTO $dbName.$tableName (
Codigo_Distrito
,Codigo_Concelho
,Codigo_Localidade
,LOCALIDADE
,ART_COD
,ART_TIPO
,PRI_PREP
,ART_TITULO
,SEG_PREP
,ART_DESIG
,ART_LOCAL
,TROCO
,PORTA
,CLIENTE
,CP4
,CP3
,CPALF
) VALUES
";
$initialInsQry = $insQry;
foreach($codigosPostais as $key => $line){
$fields = explode(';', $line);
$cod_di = $mysqli->real_escape_string($fields[0]);
$cod_co = $mysqli->real_escape_string($fields[1]);
$cod_lo = $mysqli->real_escape_string($fields[2]);
$locali = $mysqli->real_escape_string($fields[3]);
$art_co = $mysqli->real_escape_string($fields[4]);
$art_ty = $mysqli->real_escape_string($fields[5]);
$pri_pr = $mysqli->real_escape_string($fields[6]);
$art_ti = $mysqli->real_escape_string($fields[7]);
$seg_pr = $mysqli->real_escape_string($fields[8]);
$art_de = $mysqli->real_escape_string($fields[9]);
$art_lo = $mysqli->real_escape_string($fields[10]);
$troco = $mysqli->real_escape_string($fields[11]);
$porta = $mysqli->real_escape_string($fields[12]);
$client = $mysqli->real_escape_string($fields[13]);
$cp4 = $mysqli->real_escape_string($fields[14]);
$cp3 = $mysqli->real_escape_string($fields[15]);
$cpalf = $mysqli->real_escape_string($fields[16]);
$insQry .= "(
'$cod_di'
,'$cod_co'
,'$cod_lo'
,'$locali'
,'$art_co'
,'$art_ty'
,'$pri_pr'
,'$art_ti'
,'$seg_pr'
,'$art_de'
,'$art_lo'
,'$troco'
,'$porta'
,'$client'
,'$cp4'
,'$cp3'
,'$cpalf'
),\n";
// seek bar
show_status($key, $total-1);
if($key % 999 == 0 && $key > 1){ // proccess 900 at a time
#echo "$key\n";
$insQry = substr($insQry,0,-2);
$qid = $mysqli->query($insQry);
if(!$qid){
var_dump($insQry);
var_dump($mysqli->error);
die();
}else{
# echo "$key|";
# var_dump($qid);
}
$insQry = $initialInsQry;
}
}
// remove last ,\n
$insQry = substr($insQry,0,-2);
// Insert
$qid = $mysqli->query($insQry);
if(!$qid){
var_dump($mysqli->error);
}else{
var_dump($qid);
}
}