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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
| unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, StdCtrls, DB, ADODB;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Button2: TButton;
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
DataSource1: TDataSource;
Button3: TButton;
Edit3: TEdit;
ADOQuery1: TADOQuery;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
S, FileName, Fn : String;
begin
Fn := 'DB/' + Edit1.Text;
ADOConnection1.Close;
//Получаем полный путь к файлу БД с учётом текущего расположения исполняемого файла программы.
FileName := ExtractFilePath(ParamStr(0)) + Fn;
//Формируем строку соединения с учётом нового пути в файлу БД.
S :=
'Provider=Microsoft.Jet.OLEDB.4.0;'
+ 'Data Source="' + FileName + '";'
+ 'Mode=Share Deny None;'
+ 'User Id=;'
+ 'Password=;';
//Записываем новую строку соединения.
ADOConnection1.ConnectionString := S;
//Открываем БД.
ADOConnection1.Open;
ADOQuery1.Connection:=ADOConnection1;
DataSource1.DataSet:=ADOQuery1;
end;
procedure TForm1.Button2Click(Sender: TObject);
var i, j: integer;
begin
ADOTable1.TableName := Edit2.Text;
DataSource1.DataSet := ADOTable1;
ADOTable1.Active := true;
end;
function fType(Field: TField): string;
begin
if Field.DataType in [ftString, ftMemo, ftWideString, ftFixedChar] then begin // строки
result:='varchar(255)';
end else
if Field.DataType in [ftInteger, ftWord, ftSmallint, ftAutoInc, ftLargeInt] then begin // целое
result:='int';
end else
if Field.DataType in [ftFloat] then begin // дробное
result:='float';
end else
if Field.DataType in [ftDateTime] then begin // дата-время
result:='DATETIME';
end else
if Field.DataType in [ftDate] then begin // дата
result:='DATE';
end else
if Field.DataType in [ftTime] then begin // время
result:='TIME';
end else
if Field.DataType in [ftBoolean] then begin // логический
result:='bool';
end else
result:='Unknown'; // неизвеcтный
end;
procedure TForm1.Button3Click(Sender: TObject);
var
help: array of integer;
DATA: array of array of string;
TYP: array of string;
KEYCOL: array of string;
COL: array of string;
controlar: array of string;
helpstr: string;
count,m,q,w,r, i, e, j, k, l,s,z,h,z1,t, keycolum, colum, keys, control: integer;
bool, bool1, bool2 :boolean;
cntrstr, cntrstr2: string;
begin
i := ADOTable1.RecordCount;
//ShowMessage(IntToStr(i));
j := DBGrid1.Columns.Count;
//ShowMessage(IntToStr(j));
keycolum := StrToInt(Edit3.Text);
if keycolum = 1 then
begin
ShowMessage('Normalization is complite!!!');
exit;
end;
SetLength(KEYCOL,keycolum);
SetLength(COL,j);
SetLength(TYP,j);
SetLength(controlar,i);
SetLength(DATA,i,j);
SetLength(help,j);
for r:=0 to i-1 do
begin
controlar[r]:='';
end;
for r:=0 to j-1 do
begin
help[r]:=-1;
end;
with DBGrid1.DataSource.DataSet do
begin
//First;
l:=0;
while not Eof do
begin
for k := 0 to FieldCount - 1 do
DATA[l][k]:= Fields[k].AsString;
l:=l+1;
Next;
end;
end;
with DBGrid1.DataSource.DataSet do
begin
First;
for k := 0 to FieldCount - 1 do
TYP[k]:= fType(Fields[k]);
end;
with DBGrid1 do
begin
//First;
for k := 0 to keycolum-1 do
KEYCOL[k]:= Columns[k].Title.Caption;
for k := keycolum to FieldCount - 1 do
COL[k]:= Columns[k].Title.Caption;
end;
//ShowMessage( KEYCOL[0]);
//ShowMessage( KEYCOL[1]);
//ShowMessage( COL[2]);
//ShowMessage( COL[3]);
//ShowMessage( DATA[2][0] );
//ShowMessage( DATA[2][1] );
//ShowMessage( DATA[2][2] );
//ShowMessage( DATA[2][2] );
///////////////////////////////////////////
for m:=keycolum to j-1 do
begin
for q:=0 to i-1 do
begin
count:=0;
for w:=0 to i-1 do
begin
if AnsiCompareStr(DATA[q][m], DATA[w][m]) = 0 then //DATA[m][q] = DATA[m][w]
begin
count:=count+1;
end;
end;
if count > 1 then
begin
cntrstr := DATA[q][m];
for k := 0 to keycolum-1 do
begin
for e := 0 to i-1 do
begin
if AnsiCompareStr(DATA[e][m], cntrstr) = 0 then
begin
cntrstr2 := DATA[e][k];
//ShowMessage( DATA[e][k]);
break;
end;
end;
bool:=true;
for e := 0 to i-1 do
begin
if AnsiCompareStr(DATA[e][m], cntrstr) = 0 then
begin
if AnsiCompareStr(DATA[e][k], cntrstr2) <> 0 then
begin
//ShowMessage( DATA[e][k]);
bool := false;
break;
end;
end;
end;
//ShowMessage( booltostr(bool) );
if bool = true then
begin
// ShowMessage( COL[m]);
// ShowMessage( KEYCOL[k]);
// ShowMessage( TYP[m]);
// ShowMessage( TYP[k]);
help[m]:=1;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Text:= 'CREATE TABLE '+ COL[m] +
' ( ID int PRIMARY KEY,' + KEYCOL[k] + ' ' + TYP[k] + ', ' +
COL[m] + ' ' + TYP[m] + ' );';
//ShowMessage(ADOQuery1.SQL.Text );
ADOQuery1.ExecSQL;
for r:=0 to i-1 do
begin
bool2:=true;
for t:= 0 to i-1 do
begin
if AnsiCompareStr(DATA[r][m], controlar[t]) = 0 then
begin
bool2:=false;
break;//
end;
end;
if bool2 = true then
begin
controlar[r]:=DATA[r][m];
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Text:= 'INSERT INTO '+ COL[m] +
' ( ID, ' + KEYCOL[k] + ', ' + COL[m] + ' ) VALUES (' + inttostr(r) + ', ' +
''''+DATA[r][k]+'''' +', ' + ''''+DATA[r][m]+'''' + ' );';
//ShowMessage(ADOQuery1.SQL.Text );
ADOQuery1.ExecSQL;
end;
end;
//по conststr удалить лищние записи
end;
end;
break;
end;
end;
end;
////////////////////
for r:=0 to j do
begin
if help[r] = 1 then
begin
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Text:='ALTER TABLE ' + Edit2.Text + ' DROP COLUMN ' + COL[r] + ' ;';
//ShowMessage(ADOQuery1.SQL.Text );
ADOQuery1.ExecSQL;
end;
end;
ShowMessage('Normalization is complite!!!');
end;
end. |