将tcp数据保存到表数据库中

发布于 2024-10-18 04:18:36 字数 211 浏览 3 评论 0原文

我正在使用 VCL TCPServer 组件,每次有数据时它都会触发事件 在 TCP 端口上收到。在事件数据中可用文本形式 程序的参数。因为我想把这些数据保存到mysql中 我想知道解决这个问题的最佳方法是什么。 直接在过程中对收到的每个数据使用 INSERT SQL 命令 或将数据存储在内存中(即TStrings)然后调用 函数每隔 X(使用计时器)分钟执行 INSERT 命令?

谢谢

I am using a VCL TCPServer components which fires events everytime a data is
received on a tcp port. Within the event data is available into text
parameter of the procedure. Because I want to save this data into mysql
database I am wondering which is the best approach to this problem.
Directly use an INSERT SQL command within the procedure for every data received
or store the data in a memory (i.e. TStrings) and then calling
a function every X (using Timer) minutes excute the INSERT command?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阪姬 2024-10-25 04:18:36

好吧,

您不应该将数据保存在事件中,因为这样做会破坏组件执行流程。相反:

1 - 您可以缓冲要保存的所有数据,并且时不时地保存它们(不好,如果您的应用程序发生问题,您可能会丢失大量数据)。

2 - 在事件内部,创建一个线程来接收数据并将其保存到数据库中。这样,您就不会中断事件流的执行,并且可以动态保存所有数据。如果您这样做,您还将多使用一个 CPU 核心。这就是我要做的。

解决方案 2 的代码(未经测试):

TOnError = procedure(aError : string) of object;

TSaveDataThread = class(TThread)
private
  oError : string;
  oOnError : TOnError;
  oDataToSave : string;
  procedure SaveDataToDataBase;
  procedure CallOnError;
protected
  procedure Execute; override;
  constructor Create(aDataToSave : string); reintroduce;
public
  property OnError : TOnError read oOnError write oOnError;
end;

procedure TSaveDataThread.CallOnError;
begin
  oOnError(oError);
end;

constructor TSaveDataThread.Create(aDataToSave: string);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  oDataToSave := aDataToSave;
end;

procedure TSaveDataThread.Execute;
begin
  inherited;
  Self.SaveDataToDataBase;
end;

procedure TSaveDataThread.SaveDataToDataBase;
begin
  //put here your code to save aDataToSave to the database
  //set error on oError
  oError := 'error';
  Synchronize(CallOnError);
end;

procedure TForm5.OnError(aError: string);
begin
  ShowMessage(aError);
end;

procedure TForm5.Button1Click(Sender: TObject);
var
  vSaveDataThread : TSaveDataThread;
  vDataToSave : string;
begin
  //change this method fot the event you want to use
  vDataToSave := 'this is the data that will be saved to the database';
  vSaveDataThread := TSaveDataThread.Create(vDataToSave);
  vSaveDataThread.OnError := Self.OnError;
  vSaveDataThread.Start;
end;

如果您不知道线程,请查看以下内容:多线程 - Delphi 方式

Well,

You should not save your data inside the event, because you are breaking the component execution flow when you do that. Instead:

1 - You can buffer all data you want to save, and from time to time you save them (not good, you can lost a lot of data if something happens with your app).

2 - Inside the event, create a thread that will receive the data and save them to the database. This way you are not breaking the event flow execution and you will have all data saved on the fly. Also you will be using one more core of your CPU if you do so. That is what i would do.

Code for solution 2 (untested):

TOnError = procedure(aError : string) of object;

TSaveDataThread = class(TThread)
private
  oError : string;
  oOnError : TOnError;
  oDataToSave : string;
  procedure SaveDataToDataBase;
  procedure CallOnError;
protected
  procedure Execute; override;
  constructor Create(aDataToSave : string); reintroduce;
public
  property OnError : TOnError read oOnError write oOnError;
end;

procedure TSaveDataThread.CallOnError;
begin
  oOnError(oError);
end;

constructor TSaveDataThread.Create(aDataToSave: string);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  oDataToSave := aDataToSave;
end;

procedure TSaveDataThread.Execute;
begin
  inherited;
  Self.SaveDataToDataBase;
end;

procedure TSaveDataThread.SaveDataToDataBase;
begin
  //put here your code to save aDataToSave to the database
  //set error on oError
  oError := 'error';
  Synchronize(CallOnError);
end;

procedure TForm5.OnError(aError: string);
begin
  ShowMessage(aError);
end;

procedure TForm5.Button1Click(Sender: TObject);
var
  vSaveDataThread : TSaveDataThread;
  vDataToSave : string;
begin
  //change this method fot the event you want to use
  vDataToSave := 'this is the data that will be saved to the database';
  vSaveDataThread := TSaveDataThread.Create(vDataToSave);
  vSaveDataThread.OnError := Self.OnError;
  vSaveDataThread.Start;
end;

Check this out if you dont know threads: Multithreading - The Delphi Way

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文