我写的一段代码,算是核心吧,出错,帮帮我? (100分)

  • 主题发起人 主题发起人 amsea
  • 开始时间 开始时间
A

amsea

Unregistered / Unconfirmed
GUEST, unregistred user!
我写的一段代码,算是核心部分吧,出错了,谁知道如何办?
功能:保存、删除、生成完整的 SQL

{功能:保存、删除、生成完整的 SQL}
unit UTSQLOperateList;

interface
uses SysUtils, Classes, Consts, StdCtrls, upublic;

type
{一条操作包含的内容}
TSQLOperate = class
I: integer;
StrFieldName: string;
StrValue: string;
StrFieldCnName: string;
StrTableName: string;
carrySymbol: string;
Logic: string;
constructor Create;
private
public
end;

{操作列表}
TSQLOperateList = class
constructor Create; virtual;
destructor Distroy; virtual;
function GetSize(): integer;
function IsExist(FieldName: string): boolean;

private
{ Private declarations }
SO: array of TSQLOperate;

Fcount: integer;
StrHeadSQL: string;
StrTablesName: string;

function GetCount: integer;
function BuildSQL: string;
procedure SetHeadSQL(HeadSQL: string);
function GetHeadSQL: string;
procedure SetTablesName(TablesName: string);
function GetTablesName: string;

public
{ Public declarations }
function Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string): integer;
function GetCnName(index: integer): string; overload;
function GetCnName(FieldName: string): string; overload;
function GetTableName(index: integer): string; overload;
function GetTableName(FieldName: string): string; overload;
function GetFieldNames: tstringlist;
function GetRows(Index: integer): tstringlist;
function clear(): boolean;
function Delete(FieldName: string): boolean; overload;
function Delete(index: integer): boolean; overload;
property Count: Integer read GetCount;
property baseSQL: string read BuildSQL;
property TablesName: string read GetTablesName write SetTablesName;
property HeadSQL: string read GetHeadSQL write SetHeadSQL;
property SQL: string read BuildSQL;

end;

var
SQLO:TSQLOperate;
implementation

constructor TSQLOperate.Create;
begin
{初始化为空}
inherited;
end;


constructor TSQLOperateList.Create;
begin
{初始化为空}
SQLO.Create;
//^^^^^^^^^^^^^^^^^^
//错误!
//原来用的是记录,现在改成类,就有问题了。
SQLO.i:=1;
setlength(SO, 255);
Fcount := 0;
TablesName := '';
HeadSQL := 'Select * from ';
end;

destructor TSQLOperateList.Distroy;
begin
inherited Destroy;
end;

function TSQLOperateList.GetSize(): integer;
begin
{返回最大值}
result := FCount;
end;

function TSQLOperateList.Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string):
integer;
begin
{添加新的纪录}
result := FCount;
try
if not IsExist(FieldName) then
begin
so[fcount].I := fcount;
so[fcount].StrFieldName := FieldName;
so[fcount].StrValue := StrValue;
so[fcount].StrFieldCnName := FieldCnName;
so[fcount].StrTableName := TableName;
so[fcount].carrySymbol := carrySymbol;
so[fcount].Logic := Logic;
inc(FCount);
result := FCount;
end
else
begin
result := -1;
end;
except
end;
end;

function TSQLOperateList.GetCnName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := SO[index].StrFieldCnName;
end;

function TSQLOperateList.GetCnName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if SO.StrFieldName = FieldName then
result := SO.StrFieldCnName;
end;
end;

function TSQLOperateList.GetTableName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := so[index].StrTableName;
end;

function TSQLOperateList.GetTableName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
result := so.StrTableName;
end;
end;

function TSQLOperateList.Delete(FieldName: string): boolean;
var
i, h, e: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
h := i;
for e := h to count - 1 do
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
setlength(so, count - 1);
result := true;
end;
end;
end;
end;

function TSQLOperateList.Delete(index: integer): boolean;
var
e: integer;
begin
result := false;
for e := index to Count - 1 do
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
setlength(so, count - 1);
result := true;
end;
end;

function TSQLOperateList.GetFieldNames: tstringlist;
var
i: integer;
begin
result := Tstringlist.Create;
for i := 0 to Count - 1 do
begin
result.Add(so.StrTableName + '.' + so.StrFieldName + ',');
end;
end;

function TSQLOperateList.GetRows(Index: integer): tstringlist;
begin
result := Tstringlist.Create;
if (index < count) and (index >= 0) then
begin
result.Add(so[index].Logic);
result.Add(so[index].StrFieldCnName);
result.Add(so[index].carrySymbol);
result.Add(so[index].StrValue);
end;
end;

function TSQLOperateList.BuildSQL: string;
var
i: integer;
StrSQL: string;
begin
StrSQL := '';
for i := 0 to count - 1 do
begin
if StrSQL <> '' then
StrSQL := StrSQL + so.Logic + ' ';
StrSQL := StrSQL + so.StrTableName + '.' + so.StrFieldName + ' ' + so.carrySymbol + ' ';
StrSQL := StrSQL + '''' + so.StrValue + ''' ';
end;
if StrSQL <> '' then
begin
Result := HeadSQL + TablesName + ' where ' + StrSQL;
end
else
begin
Result := HeadSQL + TablesName;
end;
end;

procedure TSQLOperateList.SetHeadSQL(HeadSQL: string);
begin
StrHeadSQL := HeadSQL;
end;

function TSQLOperateList.GetHeadSQL: string;
begin
result := StrHeadSQL;
end;

procedure TSQLOperateList.SetTablesName(TablesName: string);
begin
strTablesName := TablesName;
end;

function TSQLOperateList.GetTablesName: string;
begin
Result := StrTablesName;
end;

function TSQLOperateList.IsExist(FieldName: string): boolean;
var
i: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
result := true;
exit;
end;
end;
end;

function TSQLOperateList.clear(): boolean;
begin
setlength(so, 0);
setlength(so, 255);
fcount := 0;
result := true;
end;

function TSQLOperateList.GetCount: integer;
begin
result := fcount;
end;

end.

 
Delphi中对象的创建应该这样写:
SQL0:=TSQLOperate.Create;

还有,您声明了一个全局变量:
SQLO:TSQLOperate;
不太妥当吧——无论有多少个TSQLOperateList对象,它只能操作同一个TSQLOperate对象。
为什么不在TSQLOperateList的内部定义一个TSQLOperate对象呢?
 
to creation-zy, 谢谢!!!

能帮我调一下么?
总告诉我,内存访问错误,调试察看 so = nil
 
兄弟,放弃吧。你这代码惨不忍睹。我随便能够找出30处以上的错误。
 
请barton大侠出手,让我们小菜开开眼界,
我再加100分以示谢意!!
 

先将type
{一条操作包含的内容}
TSQLOperate = class
I: integer;
StrFieldName: string;
StrValue: string;
StrFieldCnName: string;
StrTableName: string;
carrySymbol: string;
Logic: string;
constructor Create;
private
public
end;
改成type
{一条操作包含的内容}
TSQLOperate = record
I: integer;
StrFieldName: string;
StrValue: string;
StrFieldCnName: string;
StrTableName: string;
carrySymbol: string;
Logic: string;
end;
你创建的时候就不会出错了,还有你 SO: array of TSQLOperate;但你用的时候却是写死的
不如就用array[0。。254] of TSQLOperate
你的这段程序中写的很不规范,许多应该释放的变量都没有释放,希望你改正
 
to cxx1997, 我原来用的是Record,但是后来翻书都说让用class,就随便改了,
问题多不怕,就怕我这样的,不能发现问题,谢谢你~!

但是我还是想用class来实现,并且知道如何实现,为什么推荐用class来实现!

谢谢各位了~!:)
 
{一条操作包含的内容}
TSQLOperate 可见它是用来保存一系列数据的,不需要用CLASS,应该用RECORD!
 
to cxx1997,用class的原因就是为了还有其它的属性和方法,或者事件,
我这么理解,对么?

to all
我用class也不应该出错啊?是不是我还有什么问题没处理?
 
我可以帮你再改改,你可以直接和我联系
QQ:2003171
EMIAL:cxx91-94@263.net
 
功能同上,增加了一个扩展,就是将数据输出到stringgrid中。
已经调试通过。

还有一个问题不明白,为什么我用class就不行,改成record就可以了呢?
回答这个问题后,我就送分了。
最好能把这个record改成class的,来看看我到底错在哪里。

------------笑容(amsea)。
QQ:85120358

//---------------------------------------------------------------------------
{以下内容,允许自由引用,不保留版权,但是,修改后,必须通知我,
并将修改稿给我一份,谢谢了,大家一起享福嘛~!

如果不能遵守上面的话,禁止用于商业用途!!!
笑容 于 2002年01月16日
ispmaster@cnuninet.com
拷贝转载或引用,请保留本信息。
}


unit UTSQLOperateList;

interface
uses forms, SysUtils, Classes, Consts, StdCtrls, Grids;

type
TSQLOperate = record
I: integer;
StrFieldName: string;
StrValue: string;
StrFieldCnName: string;
StrTableName: string;
carrySymbol: string;
Logic: string;
end;

TSQLOperateList = class
constructor Create; virtual;
destructor Distroy; virtual;
function GetSize(): integer;
function IsExist(FieldName: string): boolean;

private
{ Private declarations }
SO: array[0..254] of TSQLOperate;

Fcount: integer;
StrHeadSQL: string;
StrTablesName: string;
Findex: integer;

function GetCount: integer;
function BuildSQL: string;
procedure SetHeadSQL(HeadSQL: string);
function GetHeadSQL: string;
procedure SetTablesName(TablesName: string);
function GetTablesName: string;
function ShowInfo(Info: string; Flags: Integer): integer;

public
{ Public declarations }
function Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string): integer;
function GetCnName(index: integer): string; overload;
function GetCnName(FieldName: string): string; overload;
function GetTableName(index: integer): string; overload;
function GetTableName(FieldName: string): string; overload;
function GetFieldNames: tstringlist;
function GetRows(Index: integer): tstringlist;
function SendToStringGrid(StringGrid: TStringGrid; BeginIndex: integer = 0): Boolean;
function clear(): boolean;
function Delete(FieldName: string): boolean; overload;
function Delete(index: integer): boolean; overload;
property Count: Integer read GetCount;
property baseSQL: string read BuildSQL;
property TablesName: string read GetTablesName write SetTablesName;
property HeadSQL: string read GetHeadSQL write SetHeadSQL;
property SQL: string read BuildSQL;

end;

implementation

const
{本单元常量}
ShowInfoTitle = 'CMS Result Information';
MB_SYSTEMMODAL = 1000;

constructor TSQLOperateList.Create;
begin
{初始化为空}
Fcount := 0;
TablesName := '';
HeadSQL := 'Select * from ';
end;

destructor TSQLOperateList.Distroy;
begin
inherited Destroy;
end;

function TSQLOperateList.ShowInfo(Info: string; Flags: Integer): integer;
begin
{MB_SYSTEMMODAL 代表系统级提示
去掉之后代表应用级提示,具体信息参见 windows单元}
Result := Application.messagebox(pchar(Info), pchar(ShowInfoTitle), flags + MB_SYSTEMMODAL);
end;

function TSQLOperateList.GetSize(): integer;
begin
{返回最大值}
result := FCount;
end;

function TSQLOperateList.Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string):
integer;
begin
{添加新的纪录}
result := FCount;
try
if IsExist(FieldName) then
begin
if showinfo('条件已经存在,选择是,用新的值代替原来的条件值;否则增加新的条件', 36) = 6 then
begin
so[Findex].I := Findex;
so[Findex].StrFieldName := FieldName;
so[Findex].StrValue := StrValue;
so[Findex].StrFieldCnName := FieldCnName;
so[Findex].StrTableName := TableName;
so[Findex].carrySymbol := carrySymbol;
so[Findex].Logic := Logic;
result := Findex;
end
else
begin
so[count].I := fcount;
so[count].StrFieldName := FieldName;
so[count].StrValue := StrValue;
so[count].StrFieldCnName := FieldCnName;
so[count].StrTableName := TableName;
so[count].carrySymbol := carrySymbol;
so[count].Logic := Logic;
inc(FCount);
result := FCount;
end;
end
else
begin
so[count].I := fcount;
so[count].StrFieldName := FieldName;
so[count].StrValue := StrValue;
so[count].StrFieldCnName := FieldCnName;
so[count].StrTableName := TableName;
so[count].carrySymbol := carrySymbol;
so[count].Logic := Logic;
inc(FCount);
result := FCount;
end;
except
end;
end;

function TSQLOperateList.GetCnName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := SO[index].StrFieldCnName;
end;

function TSQLOperateList.GetCnName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if SO.StrFieldName = FieldName then
result := SO.StrFieldCnName;
end;
end;

function TSQLOperateList.GetTableName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := so[index].StrTableName;
end;

function TSQLOperateList.GetTableName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
result := so.StrTableName;
end;
end;

function TSQLOperateList.Delete(FieldName: string): boolean;
var
i, h, e: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
h := i;
for e := h to count - 1 do
begin
if e < (count - 1) then
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
result := true;
end;
end;
dec(Fcount);
end;
end;
end;

function TSQLOperateList.Delete(index: integer): boolean;
var
e: integer;
begin
result := false;
for e := index to Count - 1 do
begin
if e < (count - 1) then
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
result := true;
end;
end;
dec(Fcount);
end;

function TSQLOperateList.GetFieldNames: tstringlist;
var
i: integer;
begin
result := Tstringlist.Create;
for i := 0 to Count - 1 do
begin
result.Add(so.StrTableName + '.' + so.StrFieldName + ',');
end;
end;

function TSQLOperateList.GetRows(Index: integer): tstringlist;
begin
result := Tstringlist.Create;
if (index < count) and (index >= 0) then
begin
result.Add(so[index].Logic);
result.Add(so[index].StrFieldCnName);
result.Add(so[index].carrySymbol);
result.Add(so[index].StrValue);
end;
end;

function TSQLOperateList.SendToStringGrid(StringGrid: TStringGrid; BeginIndex: integer = 0): Boolean;
var
i: integer;
Strings: Tstrings;
begin
Strings := Tstringlist.Create;
for i := 0 to count - 1 do
begin
Strings.Clear;
strings.Add(so.Logic);
strings.Add(so.StrFieldCnName);
strings.Add(so.carrySymbol);
strings.Add(so.StrValue);
StringGrid.rowCount := BeginIndex + i + 1;
StringGrid.Rows[BeginIndex + i] := strings;
end;
result := true;
end;

function TSQLOperateList.BuildSQL: string;
var
i: integer;
StrSQL: string;
begin
StrSQL := '';
for i := 0 to count - 1 do
begin
if StrSQL <> '' then
StrSQL := StrSQL + so.Logic + ' ';
StrSQL := StrSQL + so.StrTableName + '.' + so.StrFieldName + ' ' + so.carrySymbol + ' ';
StrSQL := StrSQL + '''' + so.StrValue + ''' ';
end;
if StrSQL <> '' then
begin
Result := HeadSQL + TablesName + ' where ' + StrSQL;
end
else
begin
Result := HeadSQL + TablesName;
end;
end;

procedure TSQLOperateList.SetHeadSQL(HeadSQL: string);
begin
StrHeadSQL := HeadSQL;
end;

function TSQLOperateList.GetHeadSQL: string;
begin
result := StrHeadSQL;
end;

procedure TSQLOperateList.SetTablesName(TablesName: string);
begin
strTablesName := TablesName;
end;

function TSQLOperateList.GetTablesName: string;
begin
Result := StrTablesName;
end;

function TSQLOperateList.IsExist(FieldName: string): boolean;
var
i: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
result := true;
Findex := i;
exit;
end;
end;
end;

function TSQLOperateList.clear(): boolean;
begin
fcount := 0;
result := true;
end;

function TSQLOperateList.GetCount: integer;
begin
result := fcount;
end;

end.
//---------------------------------------------------------------------------
 
SQLO.Create;
就这行错了,应该是SQLO := TSQLOperate.Create;
 
实际上问题是出现在数组上了,sqlo这个变量是我调试加上的,没有意义。
一个问题没弄明白,自定义的类在array中使用,需要注意些什么?
还要先建立或者如何初始化?
 
我想把数组改成TList会好很多!



unit MyClass;

interface

//试试吧!没有调试,只是的IE里面帮你改改而已!

uses forms, SysUtils, Classes, Consts, StdCtrls, Grids;

type
TSQLOperate = Class
public
I: integer;
StrFieldName: string;
StrValue: string;
StrFieldCnName: string;
StrTableName: string;
carrySymbol: string;
Logic: string;
end;

TSQLOperateList = class
function IndexOf(FieldName: string): Integer;

private
{ Private declarations }
SO: TList;

StrHeadSQL: string;
StrTablesName: string;

function GetCount: integer;
function BuildSQL: string;
procedure SetHeadSQL(HeadSQL: string);
function GetHeadSQL: string;
procedure SetTablesName(TablesName: string);
function GetTablesName: string;
function ShowInfo(Info: string; Flags: Integer): integer;

public
{ Public declarations }
constructor Create;
destructor Destroy; override;
function Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string): integer;
function GetCnName(index: integer): string; overload;
function GetCnName(FieldName: string): string; overload;
function GetTableName(index: integer): string; overload;
function GetTableName(FieldName: string): string; overload;
function GetFieldNames: tstringlist;
function GetRows(Index: integer): tstringlist;
function SendToStringGrid(StringGrid: TStringGrid; BeginIndex: integer = 0): Boolean;
function clear(): boolean;
function Delete(FieldName: string): boolean; overload;
function Delete(index: integer): boolean; overload;
property Count: Integer read GetCount;
property baseSQL: string read BuildSQL;
property TablesName: string read GetTablesName write SetTablesName;
property HeadSQL: string read GetHeadSQL write SetHeadSQL;
property SQL: string read BuildSQL;

end;

implementation

const
{本单元常量}
ShowInfoTitle = 'CMS Result Information';
MB_SYSTEMMODAL = 1000;

constructor TSQLOperateList.Create;
begin
{初始化为空}
SO:=TList.Create;
TablesName := '';
HeadSQL := 'Select * from ';
end;

destructor TSQLOperateList.Destroy;
begin
clear;
SO.Free;
inherited Destroy;
end;

function TSQLOperateList.ShowInfo(Info: string; Flags: Integer): integer;
begin
{MB_SYSTEMMODAL 代表系统级提示
去掉之后代表应用级提示,具体信息参见 windows单元}
Result := Application.messagebox(pchar(Info), pchar(ShowInfoTitle), flags + MB_SYSTEMMODAL);
end;

function TSQLOperateList.Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string):integer;
Var SQLOperate:TSQLOperate;
IDX:Integer;
begin
{添加新的纪录}
IDX:=IndexOf(FieldName);
if IDX>=0 then
begin
if showinfo('条件已经存在,选择是,用新的值代替原来的条件值;否则增加新的条件', 36) = 6 then
begin
TSQLOperate(so[IDX]^).I := IDX;
TSQLOperate(so[IDX]^).StrFieldName := FieldName;
TSQLOperate(so[IDX]^).StrValue := StrValue;
TSQLOperate(so[IDX]^).StrFieldCnName := FieldCnName;
TSQLOperate(so[IDX]^).StrTableName := TableName;
TSQLOperate(so[IDX]^).carrySymbol := carrySymbol;
TSQLOperate(so[IDX]^).Logic := Logic;
result := IDX;
end
else
begin
SQLOperate:=TSQLOperate.Create;
SQLOperate.I := Count;
SQLOperate.StrFieldName := FieldName;
SQLOperate.StrValue := StrValue;
SQLOperate.StrFieldCnName := FieldCnName;
SQLOperate.StrTableName := TableName;
SQLOperate.carrySymbol := carrySymbol;
SQLOperate.Logic := Logic;
Result:=SO.Add(SQLOperate);
end;
end
else
begin
SQLOperate:=TSQLOperate.Create;
SQLOperate.I := Count;
SQLOperate.StrFieldName := FieldName;
SQLOperate.StrValue := StrValue;
SQLOperate.StrFieldCnName := FieldCnName;
SQLOperate.StrTableName := TableName;
SQLOperate.carrySymbol := carrySymbol;
SQLOperate.Logic := Logic;
Result:=SO.Add(SQLOperate);
end;
end;

function TSQLOperateList.GetCnName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := TSQLOperate(so[index]^).StrFieldCnName;
end;

function TSQLOperateList.GetCnName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if TSQLOperate(so^).StrFieldName = FieldName then
result := TSQLOperate(so^).StrFieldCnName;
end;
end;

function TSQLOperateList.GetTableName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := TSQLOperate(so[index]^).StrTableName;
end;

function TSQLOperateList.GetTableName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if TSQLOperate(so^).StrFieldName = FieldName then
result := TSQLOperate(so^).StrTableName;
end;
end;

function TSQLOperateList.Delete(FieldName: string): boolean;
var
i: integer;
SQLOperate:TSQLOperate;
begin
result := false;
for i := 0 to count - 1 do
begin
if TSQLOperate(so^).StrFieldName = FieldName then
begin
SQLOperate:=TSQLOperate(so^);
SO.Delete(i);
SQLOperate.Free;
Result:=True;
Break;
end;
end;
end;

function TSQLOperateList.Delete(index: integer): boolean;
var
SQLOperate:TSQLOperate;
begin
result := false;
if (index>=0) and (index<count) then
begin
SQLOperate:=TSQLOperate(so[Index]^);
SO.Delete(Index);
SQLOperate.Free;
Result:=True;
end;
end;

function TSQLOperateList.GetFieldNames: tstringlist;
var
i: integer;
begin
result := Tstringlist.Create;
for i := 0 to Count - 1 do
result.Add(TSQLOperate(so^).StrTableName + '.' + TSQLOperate(so^).StrFieldName + ',');
end;

function TSQLOperateList.GetRows(Index: integer): tstringlist;
begin
result := Tstringlist.Create;
if (index < count) and (index >= 0) then
begin
result.Add(TSQLOperate(so[index]^).Logic);
result.Add(TSQLOperate(so[index]^).StrFieldCnName);
result.Add(TSQLOperate(so[index]^).carrySymbol);
result.Add(TSQLOperate(so[index]^).StrValue);
end;
end;

function TSQLOperateList.SendToStringGrid(StringGrid: TStringGrid; BeginIndex: integer = 0): Boolean;
var
i: integer;
Strings: Tstrings;
begin
Strings := Tstringlist.Create;
try
for i := 0 to count - 1 do
begin
Strings.Clear;
strings.Add(TSQLOperate(so^).Logic);
strings.Add(TSQLOperate(so^).StrFieldCnName);
strings.Add(TSQLOperate(so^).carrySymbol);
strings.Add(TSQLOperate(so^).StrValue);
StringGrid.rowCount := BeginIndex + i + 1;
StringGrid.Rows[BeginIndex + i] .Assign(strings);
end;
finally
Strings.Free;
end;
result := true;
end;

function TSQLOperateList.BuildSQL: string;
var
i: integer;
StrSQL: string;
begin
StrSQL := '';
for i := 0 to count - 1 do
begin
if StrSQL <> '' then
StrSQL := StrSQL + TSQLOperate(so^).Logic + ' ';
StrSQL := StrSQL + TSQLOperate(so^).StrTableName + '.' + TSQLOperate(so^).StrFieldName + ' ' + TSQLOperate(so^).carrySymbol + ' ';
StrSQL := StrSQL + '''' + TSQLOperate(so^).StrValue + ''' ';
end;
if StrSQL <> '' then
begin
Result := HeadSQL + TablesName + ' where ' + StrSQL;
end
else
begin
Result := HeadSQL + TablesName;
end;
end;

procedure TSQLOperateList.SetHeadSQL(HeadSQL: string);
begin
StrHeadSQL := HeadSQL;
end;

function TSQLOperateList.GetHeadSQL: string;
begin
result := StrHeadSQL;
end;

procedure TSQLOperateList.SetTablesName(TablesName: string);
begin
strTablesName := TablesName;
end;

function TSQLOperateList.GetTablesName: string;
begin
Result := StrTablesName;
end;

function TSQLOperateList.IndexOf(FieldName: string): Integer;
var
i: integer;
begin
result := -1;
for i := 0 to count - 1 do
begin
if TSQLOperate(so^).StrFieldName = FieldName then
begin
result := i;
Break;
end;
end;
end;

function TSQLOperateList.clear: boolean;
Var SQLOperate:TSQLOperate;
begin
While SO.Count>0 do
begin
SQLOperate:=TSQLOperate(SO[0]);
SO.Delete(0);
SQLOperate.Free;
end;
result := true;
end;

function TSQLOperateList.GetCount: integer;
begin
result := SO.Count;
end;

end.
 
我帮你改好了,给分吧
unit UTSQLOperateList;


interface
uses forms, SysUtils, Classes, Consts, StdCtrls, Grids;

type
TSQLOperate = class
public
I: integer;
StrFieldName: string;
StrValue: string;
StrFieldCnName: string;
StrTableName: string;
carrySymbol: string;
Logic: string;
Constructor Create; overload;
destructor Free;overload;
end;

TSQLOperateList = class

private
{ Private declarations }
SO: array[0..254] of TSQLOperate;

Fcount: integer;
StrHeadSQL: string;
StrTablesName: string;
Findex: integer;

function GetCount: integer;
function BuildSQL: string;
procedure SetHeadSQL(HeadSQL: string);
function GetHeadSQL: string;
procedure SetTablesName(TablesName: string);
function GetTablesName: string;
function ShowInfo(Info: string; Flags: Integer): integer;

public
{ Public declarations }
constructor Create; virtual;
destructor Distroy; virtual;
function GetSize(): integer;
function IsExist(FieldName: string): boolean;
function Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string): integer;
function GetCnName(index: integer): string; overload;
function GetCnName(FieldName: string): string; overload;
function GetTableName(index: integer): string; overload;
function GetTableName(FieldName: string): string; overload;
function GetFieldNames: tstringlist;
function GetRows(Index: integer): tstringlist;
function SendToStringGrid(StringGrid: TStringGrid; BeginIndex: integer = 0): Boolean;
function clear(): boolean;
function Delete(FieldName: string): boolean; overload;
function Delete(index: integer): boolean; overload;
property Count: Integer read GetCount;
property baseSQL: string read BuildSQL;
property TablesName: string read GetTablesName write SetTablesName;
property HeadSQL: string read GetHeadSQL write SetHeadSQL;
property SQL: string read BuildSQL;

end;

implementation

const
{本单元常量}
ShowInfoTitle = 'CMS Result Information';
MB_SYSTEMMODAL = 1000;

constructor TSQLOperateList.Create;
var
I:Integer;
begin
{初始化为空}
for I:=0 to 254 do
begin
SO:=TSQLOperate.Create;
end;
Fcount := 0;
TablesName := '';
HeadSQL := 'Select * from ';
end;

destructor TSQLOperateList.Distroy;
var
i:Integer;
begin
for i:=0 to 254 do
begin
SO.Free;
end;
inherited Destroy;
end;

function TSQLOperateList.ShowInfo(Info: string; Flags: Integer): integer;
begin
{MB_SYSTEMMODAL 代表系统级提示
去掉之后代表应用级提示,具体信息参见 windows单元}
Result := Application.messagebox(pchar(Info), pchar(ShowInfoTitle), flags + MB_SYSTEMMODAL);
end;

function TSQLOperateList.GetSize(): integer;
begin
{返回最大值}
result := FCount;
end;

function TSQLOperateList.Add(FieldName, StrValue, FieldCnName, TableName, carrySymbol, Logic: string):
integer;
begin
{添加新的纪录}
result := FCount;
try
if IsExist(FieldName) then
begin
if showinfo('条件已经存在,选择是,用新的值代替原来的条件值;否则增加新的条件', 36) = 6 then
begin
so[Findex].I := Findex;
so[Findex].StrFieldName := FieldName;
so[Findex].StrValue := StrValue;
so[Findex].StrFieldCnName := FieldCnName;
so[Findex].StrTableName := TableName;
so[Findex].carrySymbol := carrySymbol;
so[Findex].Logic := Logic;
result := Findex;
end
else
begin
so[count].I := fcount;
so[count].StrFieldName := FieldName;
so[count].StrValue := StrValue;
so[count].StrFieldCnName := FieldCnName;
so[count].StrTableName := TableName;
so[count].carrySymbol := carrySymbol;
so[count].Logic := Logic;
inc(FCount);
result := FCount;
end;
end
else
begin
so[count].I := fcount;
so[count].StrFieldName := FieldName;
so[count].StrValue := StrValue;
so[count].StrFieldCnName := FieldCnName;
so[count].StrTableName := TableName;
so[count].carrySymbol := carrySymbol;
so[count].Logic := Logic;
inc(FCount);
result := FCount;
end;
except
end;
end;

function TSQLOperateList.GetCnName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := SO[index].StrFieldCnName;
end;

function TSQLOperateList.GetCnName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if SO.StrFieldName = FieldName then
result := SO.StrFieldCnName;
end;
end;

function TSQLOperateList.GetTableName(index: integer): string;
begin
{根据索引获取中文}
if (index < count) and (index >= 0) then
result := so[index].StrTableName;
end;

function TSQLOperateList.GetTableName(FieldName: string): string;
var
i: integer;
begin
{根据名字获取中文}
result := '';
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
result := so.StrTableName;
end;
end;

function TSQLOperateList.Delete(FieldName: string): boolean;
var
i, h, e: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
h := i;
for e := h to count - 1 do
begin
if e < (count - 1) then
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
result := true;
end;
end;
dec(Fcount);
end;
end;
end;

function TSQLOperateList.Delete(index: integer): boolean;
var
e: integer;
begin
result := false;
for e := index to Count - 1 do
begin
if e < (count - 1) then
begin
so[e].I := so[e + 1].I;
so[e].StrFieldName := so[e + 1].StrFieldName;
so[e].StrValue := so[e + 1].StrValue;
so[e].StrFieldCnName := so[e + 1].StrFieldCnName;
so[e].StrTableName := so[e + 1].StrTableName;
so[e].carrySymbol := so[e + 1].carrySymbol;
so[e].Logic := so[e + 1].Logic;
result := true;
end;
end;
dec(Fcount);
end;

function TSQLOperateList.GetFieldNames: tstringlist;
var
i: integer;
begin
result := Tstringlist.Create;
for i := 0 to Count - 1 do
begin
result.Add(so.StrTableName + '.' + so.StrFieldName + ',');
end;
end;

function TSQLOperateList.GetRows(Index: integer): tstringlist;
begin
result := Tstringlist.Create;
if (index < count) and (index >= 0) then
begin
result.Add(so[index].Logic);
result.Add(so[index].StrFieldCnName);
result.Add(so[index].carrySymbol);
result.Add(so[index].StrValue);
end;
end;

function TSQLOperateList.SendToStringGrid(StringGrid: TStringGrid; BeginIndex: integer = 0): Boolean;
var
i: integer;
Strings: Tstrings;
begin
Strings := Tstringlist.Create;
for i := 0 to count - 1 do
begin
Strings.Clear;
strings.Add(so.Logic);
strings.Add(so.StrFieldCnName);
strings.Add(so.carrySymbol);
strings.Add(so.StrValue);
StringGrid.rowCount := BeginIndex + i + 1;
StringGrid.Rows[BeginIndex + i] := strings;
end;
result := true;
end;

function TSQLOperateList.BuildSQL: string;
var
i: integer;
StrSQL: string;
begin
StrSQL := '';
for i := 0 to count - 1 do
begin
if StrSQL <> '' then
StrSQL := StrSQL + so.Logic + ' ';
StrSQL := StrSQL + so.StrTableName + '.' + so.StrFieldName + ' ' + so.carrySymbol + ' ';
StrSQL := StrSQL + '''' + so.StrValue + ''' ';
end;
if StrSQL <> '' then
begin
Result := HeadSQL + TablesName + ' where ' + StrSQL;
end
else
begin
Result := HeadSQL + TablesName;
end;
end;

procedure TSQLOperateList.SetHeadSQL(HeadSQL: string);
begin
StrHeadSQL := HeadSQL;
end;

function TSQLOperateList.GetHeadSQL: string;
begin
result := StrHeadSQL;
end;

procedure TSQLOperateList.SetTablesName(TablesName: string);
begin
strTablesName := TablesName;
end;

function TSQLOperateList.GetTablesName: string;
begin
Result := StrTablesName;
end;

function TSQLOperateList.IsExist(FieldName: string): boolean;
var
i: integer;
begin
result := false;
for i := 0 to count - 1 do
begin
if so.StrFieldName = FieldName then
begin
result := true;
Findex := i;
exit;
end;
end;
end;

function TSQLOperateList.clear(): boolean;
begin
fcount := 0;
result := true;
end;

function TSQLOperateList.GetCount: integer;
begin
result := fcount;
end;

{ TSQLOperate }


constructor TSQLOperate.Create;
begin
Inherited Create;

end;

destructor TSQLOperate.Free;
begin
Inherited Destroy;
end;

end.
 
to cxx1997,如果要用class,在array中,就需要一个一个的去建立啊?
看来不如用record方便了。

就这个问题而言class真的没有体现出什么优势了:)
发分了。
 
多人接受答案了。
 
后退
顶部