提问:如何建一个定时器每隔一段时间自动从一个数据库提取数据…………思路,方法,最好有范例!希望回答!(100分)

  • 主题发起人 主题发起人 tianzu
  • 开始时间 开始时间
T

tianzu

Unregistered / Unconfirmed
GUEST, unregistred user!
本人菜鸟一个,今日第一次提问,望大家给予解答!谢谢!
如何建一个定时器每隔一段时间自动从一个数据库(通过BEA)临时表(如今天上网花费10圆以上的人)中提取数据,通过指定条件的筛选(如花费十圆以上中超过30圆以上的),然后把经过筛选的数据表回写到数据库!主要是定时器怎么设置???
水平太差,现在还在学习^_^,可能大家不太理解我的意思,其实我刚学,让我一下说明白也需要时间^_^,思路,方法我都想要,最好有个例子!希望大家教我一下,努力学习中…………
 
在Timer1触发时:
Timer1.Enabled := False;
try
启动事务
try
调用存储过程从源表中读记录并加入目的表,清除源表中被取走的记录
except
回滚事务
Exit;
end
提交事务
finally
Timer.Enabled := True;
end;
如果数据量大的话不如用线程处理
 
十分感谢你的解答!但是我还想多了解一点,如果数据量大的话,如何用线程处理?
为了能更好的解决这个问题我贴出一段线程的源代码,这个不是我写的,我到现在还没看懂,不过这个程序是手动输入的数据,希望大家能把看懂一些关键地方解释一下:
我的作业就是要把下面这个线程的例子,改为可每隔一段时间自动执行的:
unit U_TuxExample;
interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Db, DBTables, U_atmi, U_tmenv , U_BeaPub , U_TuxFnc;
type
TRunThread = class(TThread)
private
memo1:Tmemo;
MEMO2:tMEMO;
memo3:tmemo;
Phone1:string;
labell,label2,label3,label4:Tlabel;
wid:string;
t1,t2:Tdatetime;
protected
function CallBea(phoneno: string): string;
function b2d(Avalue:string):string;//二进制->十进制
function d2b(Avalue: string): string;//十进制->二进制
procedure Execute;
override;
public
constructor Create(memo,MEMOLOG,memosu:Tmemo;phone:widestring;LLabel,Label12,Label13,Label14:TLabel;id:string;TT:tDATETIME);
end;

TFrm_Exam_Tux = class(TForm)
ButtonTEST: TButton;
ButtonExit: TButton;
MemoText: TMemo;
Timer: TTimer;
ListBox: TListBox;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Edit1: TEdit;
Memo1: TMemo;
Label1: TLabel;
Memo2: TMemo;
Label2: TLabel;
Label3: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Button1: TButton;
OpenD: TOpenDialog;
procedure ButtonTESTClick(Sender: TObject);
procedure TimerTimer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ButtonExitClick(Sender: TObject);
procedure MemoTextChange(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
i:integer;
WORKID:STRING;
T3:tDATETIME;
Thd : array of TRunThread ;
public
{ Public declarations }
//tpinitbuf :PTPINIT;
end;

var
Frm_Exam_Tux: TFrm_Exam_Tux;
implementation
{$R *.DFM}
constructor TRunThread.Create(memo,MEMOLOG,memosu:Tmemo;phone:widestring;LLabel,Label12,Label13,Label14:TLabel;id:string;TT:tDATETIME);
begin
MEMO2:=MEMOLOG;

memo1:=memo;
memo3:=memosu;
phone1:=phone;
FreeOnTerminate := True;
labell:=llabel;
label2:=label12;
label3:=label13;
label4:=label14;
T1:=TT;
wid:=id;
inherited Create(False);
end;

procedure TRunThread.Execute;
//执行过程
var
MI_Result, MS_SendBufStr : String ;
begin
try
labell.Caption:=inttostr(strtoint(labell.Caption)+1);
MS_SendBufStr := phone1;
MI_Result := CallBea(TRIM(MS_SendBufStr));
t2:=now;
while labell.tag <> 0do
sleep(10);
labell.tag:=1;
label2.Caption:= FORMAT('%.2F',[((t2-t1)*60*60*24)]);
if pos('成功',MI_Result) <= 0 then
begin
{ if memo1.Lines.IndexOf(copy(MI_Result,1,11))<0 then
begin
Memo1.Lines.Add(copy(MI_Result,1,11));
Memo1.Lines.SaveToFile('./WriteBossError.txt');
end;
}
Memo2.Lines.Add(MI_Result);
label4.Caption:=inttostr(strtoint(label4.Caption)+1);
MessageBeep(MB_ICONQUESTION);
END
else
begin
// Memo3.Lines.Add(MI_Result);
// Memo3.Lines.SaveToFile('c:/WriteBossOk.txt');
Memo2.Lines.Add(MI_Result);
label3.Caption:=inttostr(strtoint(label3.Caption)+1);
end;
memo2.Lines.SaveToFile('./UpdateBossLog.txt');
labell.Caption:=inttostr(strtoint(labell.Caption)-1);
if strtoint(labell.Caption) <=0 then
labell.Caption:='0';
labell.tag:=0;
except
on E: Exceptiondo
begin
labell.tag:=0;
end;
end;
end;

function TRunThread.B2D(Avalue: string):string;
function NF(Avalue:integer):integer;
var
i,v_value:integer;
begin
v_value:=1;
for i:=1 to Avaluedo
begin
v_value:=v_value*2;
end;
result:=v_value;
end;
var
s:string;
i,v_value:integer;
begin
v_value:=0;
s:=Avalue;
for i:=1 to length(s) do
begin
v_value:=v_value+strtoint(s)*NF(length(s)-i);
end;
result:=inttostr(v_value);
end;

function TRunThread.d2b(Avalue: string): string;
var
v_value:integer;
s:string;
begin
s:='';
v_value:=strtoint(Avalue);
if v_value=0 then
begin
result:='00';
exit;
end;
if v_value=1 then
begin
result:='01';
exit;
end;
if v_value=2 then
begin
result:='10';
exit;
end;
if v_value=3 then
begin
result:='11';
exit;
end;
{ while v_value>2do
begin
if v_value mod 2=0 then
s:='0'+s
else
s:='1'+s;
v_value:=v_value div 2;
end;
if v_value=1 then
s:='0'+s
else
s:='10'+s;
}
result:=s;
end;

function TRunThread.CallBea(phoneno: string): string;
var
s,phone,grade,grade1:string;
MS_phoneno: String;
MS_phoneno_boss: String;
tpinitbuf :PTPINIT;
//PTPINIT = ^TTPInit;
tpinitbuf是定义的一个线程
begin
MS_phoneno := '';
MS_phoneno_boss := '';
phone := copy(phoneno,1,11);
grade := copy(phoneno,15,6);
// result:=phone;
// grade1:='1'+d2b(grade[1])+d2b(grade[2])+d2b(grade[3])+d2b(grade[4]);
//二进制转化成十进制
// grade :=b2d(grade1);
tpinitbuf :=PTPINIT(tpalloc('TPINIT', NIL, TPINITNEED(0)));
tpinitbuf.flags := TPMULTICONTEXTS;
GF_BEALOGIN(0,tpinitbuf);
//用于通知bea tuxedo中间件,本次交易即将开始,开始进行数据联路的建立
//todo:BEA事务开始
if GF_Beabegin
< 0 then
//GF_Beabegin
: bea tuxedo开始进行正式的数据交易
begin
result :=phoneno +' 服务初始化失败。';
tpfree(pChar(tpinitbuf));
GF_BEALOGOUT;
//用于通知bea tuxedo中间件,本次交易已经完毕,可以断开连接线路
Exit;
end;
//写BOSS的服务
s:=phone+#9+'*'+#9+grade+#9+'*'+#9+#9+'*'+#9+#9+'*'+#9+wid;
//TODO:CALL UPDATE_VIPFLAG
if (GF_BeaCall('VIP_UP_TO_BOSS',s)<0) then
//VIP_UP_TO_BOSS为服务名
begin
//事务RollBack
result :=phoneno + ' 服务调用失败,'+S;
GF_BeaRollBack;
//中间件连路已经建立完毕,并且交易成功,进行数据交易的提交
tpfree(pChar(tpinitbuf));
GF_BEALOGOUT;
exit;
end
else
begin
result :=phoneno + ' 服务调用成功,工单号:'+S;
end;
if Terminated then
Exit;
//事务提交。
if GF_BeaCommit<0 then
//中间件连路已经建立完毕,并且交易成功,进行数据交易的提交
begin
result :=phone + ' 服务提交失败。';
GF_BeaRollBack;
tpfree(pChar(tpinitbuf));
GF_BEALOGOUT;
exit;
end;
tpfree(pChar(tpinitbuf));
GF_BEALOGOUT;
end;

procedure TFrm_Exam_Tux.ButtonTESTClick(Sender: TObject);
begin
IF TRIM(EDIT1.Text) = '' then
begin
SHOWMESSAGE('请输入工号');
exit;
end;
workid:=trim(edit1.Text);
//workid---工号
buttontest.Enabled := false;
i:=1;
T3:=NOW;
listbox.Tag:= listbox.items.count+1;
setlength(thd,listbox.Tag);
timer.Enabled :=true;
end;

procedure TFrm_Exam_Tux.TimerTimer(Sender: TObject);
var
str:string;
begin
if strtoint(label5.caption) >= 5 then
exit;
if (ListBox.Items.Count<=0) and (memotext.Lines.Count <=0) then
begin
timer.Enabled :=false;
buttontest.Enabled := true;
exit;
end;

if (ListBox.Items.Count<=0) and (memotext.Lines.Count >0) then
begin
ListBox.Items:=memotext.Lines;
listbox.Tag:= listbox.items.count + i;
memotext.Lines.Clear;
setlength(thd,listbox.Tag);
end;

if i <= listbox.Tag then
begin
str:= ListBox.Items.Strings[0];
ListBox.Items.Delete(0);
memo2.Lines.Add(str+' 已发出请求服务');
memo2.Lines.SaveToFile('./Sended.txt');
label7.Caption:=inttostr(i);
thd:=TRunThread.Create(memotext,memo1,memo2,str,Label5,Label9,Label10,Label11,workid,T3);
thd.Resume;
i:=i+1;
end;
end;

procedure TFrm_Exam_Tux.FormCreate(Sender: TObject);
begin
if FileExists('./text.txt') then
begin
ListBox.Items.LoadFromFile('./text.txt');
timer.Interval := strtoint(trim(ListBox.Items.strings[0]));
ListBox.Items.Delete(0);
end;
end;

procedure TFrm_Exam_Tux.ButtonExitClick(Sender: TObject);
begin
if strtoint(label5.caption) > 5 then
begin
showmessage('还有线程在运行,不可以退出。');
exit;
end;
listbox.Items.Clear;
memotext.Lines.Clear;
timer.Enabled := false;
Label1.Visible := true;
label1.Refresh;
listbox.Tag:=0;
close;
end;

procedure TFrm_Exam_Tux.MemoTextChange(Sender: TObject);
begin
if (timer.Enabled =false) and (ListBox.Items.Count<=0) then
begin
timer.Enabled :=true;
buttontest.Enabled := false;
end;
end;

procedure TFrm_Exam_Tux.Button1Click(Sender: TObject);
begin
if opend.
Execute then
begin
ListBox.Items.LoadFromFile(opend.
Files.Strings[0]);
timer.Interval := strtoint(trim(ListBox.Items.strings[0]));
ListBox.Items.Delete(0);
if FileExists('./UpdateBossLog.txt') then
memo1.Lines.LoadFromFile('./UpdateBossLog.txt');
memotext.Lines.Clear;
if FileExists('./Sended.txt') then
memo2.Lines.LoadFromFile('./Sended.txt');
end;
end;

procedure TFrm_Exam_Tux.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
memo2.Lines.Clear;
end;

end.

 
啊!!今天都没有人帮我!其实只要点播我一下,或者给各相关的例子就行,最好提示一下该注意的事项!谢谢各位了!
早知道代码这么多就不贴上来了,害的没人来看………………
 
接受答案了.
 
后退
顶部