-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataModuleUnit.pas
167 lines (151 loc) · 4.94 KB
/
DataModuleUnit.pas
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
unit DataModuleUnit;
interface
uses
SysUtils, Classes, DB, ADODB, Dialogs;
type
TDataModule1 = class(TDataModule)
MainConnection: TADOConnection;
MainQuery: TADOQuery;
MainDataSource: TDataSource;
DBQuery: TADOQuery;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
//ñïèñêè çíà÷åíèé ãðàô òàáëèöû
ages_list: TStringList;
belts_list: TStringList;
weights_list: TStringList;
cities_list: TStringList;
clubs_list: TStringList;
function fulfill_list(tableName: string; colName: string; var list:TStringList):integer; //ôóíêöèÿ çàïîëíåíèÿ ñïèñêîâ çíà÷åíèÿìè èõ ÁÄ
//tableName: string - èìÿ òàáëèöû èç êîòîðîé áåðåì çíà÷åíèÿ; colName: string - èìÿ ñòîëáöà; var list:TStringList - ñïèñîê â êîòîðûé áóäåì âûãðóæàòü äàííûå; âîçâðàùàåò 0, åñëè âñå ïðîøëî ãëàäêî, 1 åñëè íå ïîäêëþ÷èëñÿ ê ÁÄ, 2, åñëè ïóñòîå çíà÷åíèå
function add_value_query(table,colName:string; value:variant): integer; //çàïðîñ äëÿ äîáàâëåíèÿ
function add_weight_query(weight,age: variant): integer; //çàïðîñ äëÿ äîáàâëåíèÿ âåñà
function find_value_querry(table,colName,searchColName:string; param:variant; var value:variant):integer; //ôóíêöèÿ ïîèñêà çíà÷åíèÿ ñòîëáöà çàïèñè
//table - èìÿ òàáëèöû ïîèñêà,colName - ñòîëáåö â êîòîðîì èñõîäíîå çíà÷åíèå,searchColName - ñòîëáåö, â êîòîðîì èñêîìîå çíà÷åíèå; param - èñõîäíîå çíà÷åíèå; var value - íóæíîå çíà÷åíèå
end;
var
DataModule1: TDataModule1;
implementation
{$R *.dfm}
{ TDataModule1 }
function TDataModule1.fulfill_list(tableName, colName: string;
var list: TStringList): integer;
begin
list.Clear;
DBQuery.SQL.Text :='SELECT * FROM ' + tablename;
try
DBQuery.Open;
if not DBQuery.IsEmpty then
begin
DBQuery.First;
While not DBQuery.Eof do //íåïóñòîé çàïðîñ, çíàû÷èò, çàïîëíÿåì ñòðèíãëèñò
begin
list.Add(DBQuery.FieldByName(colName).AsString);
DBQuery.Next;
end;
result:= 0;
end
else
result := 2;
except
result:= 1;
end;
DBQuery.Close;
end;
procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
//îðïåäåëÿåì ñîåäèíåíèå
MainConnection.Close; //çàêðûâàåì, ò.ê. íà îòëàäêå îòêðûò
MainConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=.\DB\Base.mdb;Mode=Share Deny None';
try
MainConnection.Open;
MainQuery.Open;
except
showmessage('Ïðîáëåìû ñ ÁÄ. Èñïðàâüòå áàçó è ïåðåçàïóñòèòå ïðîãðàììó');
Exit;
end;
//îïðåäåëÿåì è çàïîëíÿåì ñïèñêè(Âåñ íå âõîäèò, ïîòîìó ÷òî îòäåëüíûé äëÿ êàæäîãî âîçðàñòà)
ages_list := TStringList.Create;
if fulfill_list('t_ages', 'age', ages_list) <> 0 then
ShowMessage('Òàáëèöà âîçðàñòîâ ïóñòà ëèáî îòñóòñòâóåò');
belts_list := TStringList.Create;
if fulfill_list('t_belts', 'belt', belts_list) <> 0 then
ShowMessage('Òàáëèöà ïîÿñîâ ïóñòà ëèáî îòñóòñòâóåò');
weights_list := TStringList.Create;
{if fulfill_list('t_weights', 'weight', weights_list) <> 0 then
ShowMessage('Òàáëèöà âåñîâ ïóñòà ëèáî îòñóòñòâóåò');}
cities_list := TStringList.Create;
if fulfill_list('t_cities', 'city', cities_list) <> 0 then
ShowMessage('Òàáëèöà ãîðîäîâ ïóñòà ëèáî îòñóòñòâóåò');
clubs_list := TStringList.Create;
if fulfill_list('t_clubs', 'club', clubs_list) <> 0 then
ShowMessage('Òàáëèöà êëóáîâ ïóñòà ëèáî îòñóòñòâóåò');
end;
function TDataModule1.add_value_query(table, colName: string;
value: variant): integer;
begin
DBQuery.Close;
DBQuery.SQL.Text := 'SELECT * FROM ' + table;
try
DBQuery.Open;
DBQuery.Insert;
DBQuery.FieldByName(colName).AsVariant := value;
DBQuery.Post;
DBQuery.Close;
result := 0;
except
result := 1;
end
end;
function TDataModule1.add_weight_query(weight, age: variant): integer;
var id:variant;
r:integer;
begin
r := find_value_querry('t_ages','age','id',age, id);
if r <> 0 then //ÂÛÕÎÄÈÌ, ÅÑËÈ ÍÅ ÏÎËÓ×ÈËÈ id
result := r
else
begin
DBQuery.Close;
DBQuery.SQL.Text := 'SELECT * FROM t_weights';
try
DBQuery.Open;
DBQuery.Insert;
DBQuery.FieldByName('weight').AsVariant := weight;
DBQuery.FieldByName('age').AsVariant := id;
DBQuery.Post;
result := r; //òàì óæå 0
except
result := 3;
end;
DBQuery.Close;
end
end;
function TDataModule1.find_value_querry(table, colName,
searchColName: string; param: variant; var value: variant): integer;
begin
DBQuery.Close;
DBQuery.SQL.Text := 'SELECT * FROM ' + table + ' WHERE ' + colName + ' = ' + QuotedStr(param);
try
DBQuery.Open;
if not DBQuery.IsEmpty then
begin
value := DBQuery.FieldByName(searchColName).AsVariant;
result := 0;
end
else
result := 2;
except
result := 1;
end;
DBQuery.Close;
end;
procedure TDataModule1.DataModuleDestroy(Sender: TObject);
begin
MainConnection.Connected := false; //îòêëþ÷àåìñÿ îò áàçû
end;
end.