大侠们,delphi7如何编写一个读取以逗号隔开的数到数据库啊?
在memo文本框中,011921,这三个数为一组对应数据库中一行;最好能够有一个时钟,动态的存储(来了数就存到数据库中)...
在memo文本框中,01 19 21 ,这三个数为一组对应数据库中一行;
最好能够有一个时钟,动态的存储(来了数就存到数据库中) 展开
最好能够有一个时钟,动态的存储(来了数就存到数据库中) 展开
展开全部
//定义一个全局变量
Gl_i_RowCount:integer;
//窗体显示事件中Gl_i_RowCount:=0
//添加SaveRecByteToDB到memo Change中去
function SaveRecByteToDB():integer;
var
S,Str_SQL:string;
Str_Field1,Str_Field2,Str_Field3:string;
sl_Datalist:TStringlist;
begin
Result:=0;
S:=mmo1.Text;
S:=stringreplce(S,' ','',[replaceall]); //先去掉空格
sl_Datalist.CommText=S;
if (sl_Datalist.Count>Gl_i_RowCount) and
(Length(sl_Datalist.strings[sl_Datalist.Count-1])=6) then
begin
Gl_i_RowCount:=Gl_i_RowCount+1;
Str_Field1:=Copy(S,1,2);
Str_Field2:=Copy(S,3,2);
Str_Field3:=Copy(S,5,2);
Str_SQL:=' Insert into Table(Field1,Field2,Field3) values('+
''''+Str_Field1+''''+','+
''''+Str_Field2+''''+','+
''''+Str_Field3+''''+')';
if ExecSQL(Str_SQL)<0 then //你自己写提交SQL函数,最基本的
begin
Result:=-1;
ShowMessage(Str_SQL);
Exit;
end;
end;
end;
展开全部
先给个分割函数
function Split(pString:Pchar;psubString:PChar):TStringList;
var
nSize,SubStringSize:DWord;
intI,intJ,intK:DWORD;
ts:TStringList;
curChar:Char;
strString:string;
strsearchSubStr:string;
begin
nSize:=strLen(pString);
SubStringSize:=strLen(PSubString);
ts:=TStringList.Create;
strstring:='';
inti:=0;
while intI<=(nSize-1) do
begin
if (nsize-inti)>= substringSize then
begin
if ((PString+intI)^=pSubString^) then
begin
intk:=inti;
strSearchSubStr:='';
curchar:=(pstring+intk)^;
strsearchSubStr:=strSearchSubStr+Curchar;
intk:=intk+1;
for intj:= 1 to SubStringSize-1 do
begin
if ((pString+intk)^=(PSubString+intj)^) then
begin
curchar:=(pstring+intk)^;
intk:=intk+1;
strsearchSubStr:=strSearchSubStr+Curchar;
end
else begin
inti:=intk;
strString:=strString+strSearchSubStr;
break; //不匹配 退出FOR
end;
end;
if (intJ=substringSize) or (SubStringSize=1) then
begin
inti:=intk;
ts.add(strstring);
strstring:='';
end;
end
else begin
curChar:=(pString+inti)^;
strstring:=strstring+curchar;
inti:=inti+1;
end;
if inti=nsize then
begin
ts.Add(strString);
strString:='';
end;
end
else begin //将剩下的字符给作为一个字符串复制给字符串集合
strString:=strstring+string(pString+inti);
ts.Add(strstring);
inti:=nsize;
end;
end;
Result:=ts;
end;
假如str为接收的字符串
str:='01 19 21,.....';
var
s1,s2:Tstringlist;
i:integer;
tmpstr:string;
s1:=Split(pchar(str),','); //先按分号分割
adoquery.close;
adoquery.sql.text:='select * from table';
adoquery.open;
for i:=0 to s1.count-1 do
begin
tmpstr:=s1.Strings[i]; //取3个数据
s2:=Split(pchar(tmpstr),' '); //按空格分割
adoquery.append;
adoquery.fieldbyname('字段1').Value:=s2.Strings[0];
adoquery.fieldbyname('字段2').Value:=s2.Strings[1];
adoquery.fieldbyname('字段3').Value:=s2.Strings[2];
adoquery.post;
end;
adoquery.close;
每次接收到数据 就执行一下以上代码
function Split(pString:Pchar;psubString:PChar):TStringList;
var
nSize,SubStringSize:DWord;
intI,intJ,intK:DWORD;
ts:TStringList;
curChar:Char;
strString:string;
strsearchSubStr:string;
begin
nSize:=strLen(pString);
SubStringSize:=strLen(PSubString);
ts:=TStringList.Create;
strstring:='';
inti:=0;
while intI<=(nSize-1) do
begin
if (nsize-inti)>= substringSize then
begin
if ((PString+intI)^=pSubString^) then
begin
intk:=inti;
strSearchSubStr:='';
curchar:=(pstring+intk)^;
strsearchSubStr:=strSearchSubStr+Curchar;
intk:=intk+1;
for intj:= 1 to SubStringSize-1 do
begin
if ((pString+intk)^=(PSubString+intj)^) then
begin
curchar:=(pstring+intk)^;
intk:=intk+1;
strsearchSubStr:=strSearchSubStr+Curchar;
end
else begin
inti:=intk;
strString:=strString+strSearchSubStr;
break; //不匹配 退出FOR
end;
end;
if (intJ=substringSize) or (SubStringSize=1) then
begin
inti:=intk;
ts.add(strstring);
strstring:='';
end;
end
else begin
curChar:=(pString+inti)^;
strstring:=strstring+curchar;
inti:=inti+1;
end;
if inti=nsize then
begin
ts.Add(strString);
strString:='';
end;
end
else begin //将剩下的字符给作为一个字符串复制给字符串集合
strString:=strstring+string(pString+inti);
ts.Add(strstring);
inti:=nsize;
end;
end;
Result:=ts;
end;
假如str为接收的字符串
str:='01 19 21,.....';
var
s1,s2:Tstringlist;
i:integer;
tmpstr:string;
s1:=Split(pchar(str),','); //先按分号分割
adoquery.close;
adoquery.sql.text:='select * from table';
adoquery.open;
for i:=0 to s1.count-1 do
begin
tmpstr:=s1.Strings[i]; //取3个数据
s2:=Split(pchar(tmpstr),' '); //按空格分割
adoquery.append;
adoquery.fieldbyname('字段1').Value:=s2.Strings[0];
adoquery.fieldbyname('字段2').Value:=s2.Strings[1];
adoquery.fieldbyname('字段3').Value:=s2.Strings[2];
adoquery.post;
end;
adoquery.close;
每次接收到数据 就执行一下以上代码
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var
s: Tstrings;
begin
s := Tstringlist.Create ;
s.DelimitedText := 'd,b,c,e';
s.Delimiter := ',';
memo1.Lines.Assign(s);
end;
s: Tstrings;
begin
s := Tstringlist.Create ;
s.DelimitedText := 'd,b,c,e';
s.Delimiter := ',';
memo1.Lines.Assign(s);
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询