想返回一个这样的子串,要如何实现?(170分)

  • 主题发起人 主题发起人 yybug
  • 开始时间 开始时间
Y

yybug

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个这样的字符串“select student.stu_no as "学号", student.stu_name as "姓名",ach.ach_num as "成绩",ach.sir as "任课老师",......”

若我用 学号 ,作为条件,我想返回字符串 student.stu_no as "学号"
若我用 姓名 ,作为条件,我想返回字符串 student.stu_name as "姓名"
若我用 任课老师 ,作为条件,我想返回字符串 ach.sir as "任课老师"
....
这样的匹配算法,奇怪吧?
望高人指点!!
函数尽量简单,我的程序已经太臃肿了!!
 
如果你用什么作条件,就用不同的查询不就行了
通过程序来控制不是很简单吗,也许没搞清楚你到底要怎样
 
如果是用SQL SERVER, 比较简单, 先在
后台建立一个视图view:
select stu_no as 学号, ....as ....,
........ from ....
然后以后在前台就这样查询:
select * from .... where 学号='.....' 就可以了
修改:
update ... set 学号='....' where 学号='.....'
 
因为你的要求都是动态的,要是我的话我就建立一个对照表:
ID Display TableName FieldName
1 学号 student stu_no
2 姓名 student stu_name
3 任课老师 ach sir

然后你从这个表里面选择就行了,如传入的是“学号”
select TableName||'.'||FieldName||' as "'||Display||'"' from 对照表
where Display = '学号'
返回的结果就是你要的“student.stu_no as "学号"”
用其它也是一样,当把所有子串取得后,拼接起来就是一个SQL啦。
 
你们把意思理解错了,我的表达能力太差
我是对字符串操作。
我想做一个函数:输入2个字符串
一个:select student.stu_no as "学号", student.stu_name as "姓名",ach.ach_num as "成绩",ach.sir as "任课老师",......
一个:学号
要求 返回另一个字符串:student.stu_no as "学号"

这个函数,怎么写?
 
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:=getStr('select student.stu_no as "学号", student.stu_name as "姓名",ach.ach_num as "成绩",ach.sir as "任课老师"','姓名');
end;

function TForm1.getStr(resStr, subStr: String): String;
var
i,j,m,strPos,resStrLen,dPos:Integer;
tmpStr1,tmpStr2,tmpStr3:String;
begin
resStrLen:=Length(resStr);
strPos:=Pos(subStr,resStr);
tmpStr1:='';
tmpStr2:='';
dPos:=0;
for i := 0 to resStrLen-strPos-1 do
begin
if resStr[strPos+i]=',' then begin
tmpStr1:=copy(resStr,strPos,i);
break;
end;
end;
for j := 0 to strPos do
begin
if resStr[strPos-j]='.' then begin
tmpStr2:=copy(resStr,strPos-j,j);
dPos:=strPos-j;
break;
end;
end;
for m := 0 to dPos do
begin
if resStr[dPos-m]=' ' then begin
tmpStr3:=copy(resStr,dPos-m+1,m-1);
break;
end;
end;
Result:=tmpStr3+tmpStr2+tmpStr1;
end;
 
我要做这个函数的原因是:
我写了一个这样的sql语句:select stu_no as "学号", ....as ....,
........ from ....
现在我通过读dbgrid的字段名,得到的字段名是"学号",而不是student.stu_no。
而我现在想使用这个字段名,构成一个这样的sql:
select stu_no as "学号" from .....

但我总不能写成:select 学号 from .....
在这里我还是能通过读query.sql.text ,得到完整的以前的sql语句。
所以就只能做一个函数,输入: 原始的完整的sql 和 学号(或者别的)
返回这样的字符串: stu_no as "学号"
这个函数怎么写?

谢谢指教了!
 
function GetOriginFieldName(const aSelectText: string; const aFieldAlias: string): string;
var

begin

end;
 
function GetOriginFieldName(const aSelectText: string; const aFieldAlias: string): string;
const
vLenOfSelectKeyword = Length('select');
var
vPos: Integer;
vEndPos: Integer;

procedure SkipIdentifier;
begin
while UpCase(aSelectText[vPos]) in ['A'..'Z', '_', '0', '9'] do
Dec(vPos);
end;

procedure SkipDotIdentifier;
begin
Dec(vPos); //skip dot
if vEndPos = vLenOfSelectKeyword then
raise Exception.Create('Syntax error.');

SkipIdentifier;
if aSelectText[vPos] = '.' then
SkipDotIdentifier;
end;

begin
vPos := pos(aFieldAlias, aSelectText);
if vPos < vLenOfSelectKeyword then
raise Exception.Create('Error');

Dec(vPos);
if aSelectText[vPos] = '"' then
Dec(vPos);

while aSelectText[vPos] = ' ' do
Dec(vPos);

if (UpCase(aSelectText[vPos-1]) = 'A')
and (UpCase(aSelectText[vPos]) = 'S') then
Dec(vPos, 2)
else
raise Exception.Create('Error');

while aSelectText[vPos] = ' ' do
Dec(vPos);

if (not (UpCase(aSelectText[vPos]) in ['A'..'Z', '_', '0', '9']))
or (vEndPos = vLenOfSelectKeyword) then
raise Exception.Create('Except identifier');

vEndPos := vPos;

SkipIdentifier;

if aSelectText[vPos] = '.' then
SkipDotIdentifier;

Result := Copy(aSelectText, vPos+1, vEndPos-vPos)
+ Format(' as "%s"', [aFieldAlias]);
end;
 
你可以换一种办法,
cLst, eLst: TStrings;
cLst存储查询出来的字段中文名称
eLst存储原来的英文字段名
一一对应。
根据中文字段名,就知道英文的了,呵呵
 
很简单的字符串取子串功能嘛, 一次循环搞定。
function GetFieldName(const ASql, ChangedName: string): string;
var
b, e: Integer;
begin
b := 7; // 假设ASql以'Select '开头
e := 7;
while e <= length(ASql)-Length(ChangedName)+1 do
begin
if strlicomp(@(ASql[e]), ' FROM ', 6)=0 then break;
if strlicomp(@(ASql[e]), pchar(ChangedName), length(ChangedName)) = 0 then
begin
Inc(e, length(ChangedName));
while (e<=length(ASql)) and (ASql[e]<>',') and (not (ASql[e] in ['F','f']) or (strlicomp(@(ASql[e]), 'From ')=0)) do
Inc(e);
result := Trim(copy(ASql, b, e-b));
Exit;
end;
if ASql[e] = ',' then b := e+1;
Inc(e);
end;
Result := '';
end;
 
function Tform1.getstr1(resStr:String;SubStr:String):String;
var
myStr:String;
i,j,st:Integer;
subStrlist: array[1..300] of string;
ConDo:Boolean;
begin
resStr:=Trim(resStr);
SubStr:=Trim(SubStr);
st:=POS('SELECT',UPPERCase(resStr));
if st<>0 then
myStr:=copy(resStr,st+6,Length(resStr)-6);
st:=POS('FROM',UPPERCase(myStr));
if st<>0 then
myStr:=Copy(myStr,1,st-1);
ConDo:=True;
i:=1;
st:=1;
while ConDo do begin
st:=POS(',',myStr);
if st=0 then begin
SubStrList:=myStr;
ConDo:=False;
end
else
SubStrList:=copy(myStr,1,st-1);
i:=i+1;
myStr:=copy(myStr,st+1,Length(mystr)-st);

end;
for j:=1 to i do begin
if POS(SubStr,SubStrList[j])<>0 then begin
Result:=' '+SubStrList[j]+' ';
Exit;
end ;
end;
Result:='';
end;
 
后退
顶部