-
Notifications
You must be signed in to change notification settings - Fork 0
/
uSQLUtils.pas
92 lines (75 loc) · 1.68 KB
/
uSQLUtils.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
unit uSQLUtils;
interface
uses
Classes, ADODB, IOUtils,
uDatabase, System.SysUtils;
type
TSQLHelper = class(TAdoQuery)
public
constructor Create(aDatabaseName: string);
destructor Destroy; override;
procedure Open(aSql: string); overload;
function SaveToExcel(IncludeHeader: Boolean): string;
private
FDatabaseName: string;
FSQLDataModule: TSqlDatabase;
end;
implementation
{ TSQLHelper }
constructor TSQLHelper.Create(aDatabaseName: string);
begin
inherited Create(nil);
FDatabaseName := aDatabaseName;
FSQLDataModule := TSqlDataModule.Create(nil);
FSQLDataModule.Connect(aDa
tabaseName);
Connection := FSQLDataModule.AdoSqlConnection;
end;
destructor TSQLHelper.Destroy;
begin
Close;
FSQLDataModule.Free;
inherited;
end;
procedure TSQLHelper.Open(aSql: string);
begin
SQL.Text := aSql;
try
Open;
except
on e: Exception do
raise Exception.Create(e.Message);
end;
end;
function TSQLHelper.SaveToExcel(IncludeHeader: Boolean): string;
var
aRecno: Integer;
sw: TStreamWriter;
S: string;
I: Integer;
procedure WriteHeader;
var
I: Integer;
begin
for I := 0 to Fields.Count - 1 do
S := S + Fields[I].FieldName + ',';
S := S.Remove(S.Length - 1) + #13#10;
TFile.AppendAllText(Result, S);
end;
begin
Result := IncludeTrailingBackslash(ExtractFilePath(ParamStr(0))) + FDatabaseName + '.csv';
if IncludeHeader then
WriteHeader;
aRecno := RecNo;
First;
while not Eof do begin
S := '';
for I := 0 to FieldCount - 1 do
S := S + Fields[I].AsString + ',';
S := S.Remove(S.Length - 1) + #13#10;
TFile.AppendAllText(Result, S);
Next;
end;
Recno := aRecno;
end;
end.