databasename:添入DataBase名称,跟TDataBase中的数据库名一样,如BS_OLAP
tablename:要查询的表名;
fieldlist:字段列表;以逗号分割;如'mobile_num,custom_name,custom_addr'
fieldmaplist:字段中文名称,以逗号分割,跟fieldlst对应的字段中文名
如'手机号码,客户名称,客户地址'
fieldmodify:需要修改的字段列表;该列表中的字段必须在fieldlist中存在,否则会被删除
如仅需修改 'custom_name,custon_addr'
keyfield:主键字段列表
var i:integer;
sSQL:string;
sSQLUpdate:String;
sSQLDelete:String;
sSQLInsert:String;
sSQLWhereList:string;
sSQLUpdateValueSet:string;
sSQLInsertFieldList:string;
sSQLInsertValueList:string;
FieldName:String;
begin
//Query选择数据库名称
QueryARPU.Close;
QueryARPU.DatabaseName:=databasename;
//保存要查询的表名
QueryTableName:=tablename;
//建立字段列表
if not assigned(FieldNameLst) then
FieldNameLst:=TStringList.Create;
FieldNameLst.Clear;
FieldNameLst.CommaText:=fieldlist;
//建立字段中文名列表
if not assigned(ChineseLst) then
ChineseLst:=TStringList.Create;
ChineseLst.Clear;
ChineseLst.CommaText:=fieldmaplist;
//建立修改字段列表
if not assigned(ModifyFieldLst) then
ModifyFieldLst:=TStringList.Create;
ModifyFieldLst.Clear;
ModifyFieldLst.CommaText:=fieldmodify;
//确保所有要修改的字段都包含在FieldNameLst列表中
for i:=ModifyFieldLst.Count-1do
wnto 0do
begin
if FieldNameLst.IndexOf(ModifyFieldLst.Strings
)=-1 then
ModifyFieldLst.Delete(i);
end;
//建立主键字段列表
if not assigned(KeyFieldLst) then
KeyFieldLst:=TStringList.Create;
KeyFieldLst.Clear;
KeyFieldLst.CommaText:=keyfield;
//确保所有主键字段都包含在FieldNameLst列表中
for i:=KeyFieldLst.Count-1do
wnto 0do
begin
if FieldNameLst.IndexOf(KeyFieldLst.Strings)=-1 then
KeyFieldLst.Delete(i);
end;
//组合查询,更新,添加,删除SQL语句
sSQLWhereList:='';
sSQLUpdateValueSet:='';
sSQLInsertFieldList:='';
sSQLInsertValueList:='';
for i:=0 to ModifyFieldLst.Count-1do
begin
FieldName:=ModifyFieldLst.Strings;
if sSQLWhereList<>'' then
sSQLWhereList:=sSQLWhereList+' and ';
sSQLWhereList:=sSQLWhereList+FieldName+'=ld_'+FieldName;
if sSQLUpdateValueSet<>'' then
sSQLUpdateValueSet:=sSQLUpdateValueSet+',';
sSQLUpdateValueSet:=sSQLUpdateValueSet+FieldName+'=:'+FieldName;
if sSQLInsertFieldList<>'' then
sSQLInsertFieldList:=sSQLInsertFieldList+',';
sSQLInsertFieldList:=sSQLInsertFieldList+FieldName;
if sSQLInsertValueList<>'' then
sSQLInsertValueList:=sSQLInsertValueList+',';
sSQLInsertValueList:=sSQLInsertValueList+':'+FieldName;
end;
//生成SQL查询语句
sSQL:='select '+ fieldlist+' from '+ TableName;
SQLSetSQL(QueryARPU,sSQL);
//生成更新,添加,删除SQL语句
fmtstr(sSQLUpdate,'Update %s set %s Where %s',[TableName,sSQLUpdateValueSet,sSQLWhereList]);
fmtstr(sSQLInsert,'Insert into %s ( %s ) values ( %s )',[TableName,sSQLInsertFieldList,sSQLInsertValueList]);
fmtstr(sSQLDelete,'Delete from %s where %s',[TableName,sSQLWhereList]);
//更新CacheUpdate控件的更新,添加,删除SQL语句
With UpdateSQLARPUdo
begin
ModifySQL.Clear;
ModifySQl.Add(sSQLUpdate);
DeleteSQL.Clear;
DeleteSQL.Add(sSQLDelete);
InsertSQL.Clear;
InsertSQL.Add(sSQLInsert);
end;
你自己看看,整理整理;