Delphi:以某种结构存储数据 [英] Delphi: Store data in somekind of structure

查看:33
本文介绍了Delphi:以某种结构存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我在 Delphi 2010 中工作的模拟程序.模拟不是问题,但我需要使用大量数据,这会产生问题.数据在 Excel 表中可用,因此无需在 Delphi 中编辑此数据,但从 excel 表中收集此数据大约需要 10 分钟.只要您不需要在每次程序运行时都收集数据,这不是问题.所以我做了一个程序,它收集所有的数据使它可见,这里没有问题,然后存储它.但是我不能将它存储为Delphi 格式",而不会丢失结构,所以它可以在几秒钟内加载.

For a simulation program I'm working in Delphi 2010. The simulation isn't a problem but I need to use large collection of data which gives a problem. The data is available in excel sheets, so there is no need to edit this data in Delp but collecting this data from the excel sheets takes around 10min. This isn't a problem as long as you don't need to collect the data every time the program runs. So I made a program which collects all the data makes it visible, not problems here,and then store it. However I can't store it to a "Delphi format" , without losing the structure, so it can be loaded in a few seconds.

我在 Delphi 方面没有那么有经验,我搜索了很长时间的解决方案,但不明白什么是最好的.我认为我构建数据的方式是错误的,但它既简单又有效.但是,如果有更好的存储数据的方法,请说出来,但请记住,我需要更多的解释,而不仅仅是使用xml 文件"、generict"或Ttreeview".(已阅读但无法使用).

I'm not that experienced in Delphi and I searched a long time for the solution but couldn't understand what was best. I think my way of structuring the data is wrong but it was simple and worked. However if there are better ways of storing the data please say so, but remember that I need some more explanation than just use 'a xml file', 'generict, or 'Ttreeview'. (have read it but wasn't able to use it).

数据是:我做了这个产品,下一个我做的产品是这个,我需要清洗吗?对或错.

The data is for: I made this product, The next product I make is this, so do I need to clean? True or false.

数据存储为带有 Productnumber(整数)的类(TObject)和一个包含接下来可以制作的所有产品的列表.此列表包含另一个带有 Productnumber(整数)的类(TObject)和我需要的清理(​​布尔值).我想将此结构保存在一个文件中,而不会丢失数据并将其读回相同的结构.

The data is stores as a class(TObject) with Productnumber (integer) and a List which contains all products that could be made next.This list contains another class(TObject) with an Productnumber (integer) and a do I need to clean(boolean). I want to save this structure in a file, without losing the data and read it back to the same structure.

我希望有人可以提供帮助.提前谢谢你.

I hope someone could help. Thank you in advance.

更新:提供更多信息的代码(修改为英文)

Update: The code to provide a little more information (modified to English)

Clean_from = class(TObject)
public
  myfromNumber      : Integer;
  mylist            : TList;
published
  constructor Create;
End

Clean_To = class(TObject)
public
  myToNumber        : Integer;
  Clean             : Boolean;
End;

constructor Clean_from.Create;
begin
  inherited Create;
  myList := Tlist.Create;
end;

For i = 0 to 100 do
begin
  From:= Clean_from.create;
  for j := 0 to 10 do 
  begin 
    To := Clean_To.create;
    To.clean := true or false;
    From.myList.add(To);
  end;
  GlobalList.add(from);
end;

现在我想保存包含所有内容的全局列表,以便可以使用相同的结构加载它.

And now I want to save the global list with all the content so I could load it with the same structure.

推荐答案

你需要的是所谓的序列化"机制.

What you need is the so-called "serialization" mechanism.

1.标准方式

1.1 SaveToStream

在Delphi中,我们通常实现一个SaveToStream方法,它将每个对象的内容保存在一个目标TStream中(或者是一个TFileStreamTMemoryStream).

In Delp we usually implement a SaveToStream method, which will save the content of each object in a destination TStream (either a TFileStream or a TMemoryStream).

您必须手动编写序列化.

You'll have to write the serialization by hand.

1.2 类 DFM 流媒体

参见 TWriter/TReader 类.

如果您在已发布的属性中定义数据,则可以使用那些标准的 Delphi 类对它们进行序列化.

If you define your data in published properties, you are able to serialize them using those standard Delphi classes.

有关能够将任何 TCollection 序列化到 JSON 内容的方法,请参阅 这篇博文.

For some methods able to serialize any TCollection to and from JSON content, see this blog article.

<强>2.RTTI

例如查看 这个 SO 问题.

特别是,新的增强型 RTTI(自 Delphi 2010 起提供)为序列化开辟了新的机会.

In particular, the new enhanced RTTI (available since Delphi 2010) opens new opportunities to serialization.

3.使用记录而不是类

如果每个项目不存储大量内容(一些整数/布尔值),则使用记录而不是对象可能是有意义的.对于速度和内存消耗/碎片,这可能是值得的.

If each item does not store a lot of content (some integer/boolean), it may make sense to use records instead of objects. For speed and memory consumption/fragmentation, it may be worth it.

这里是 一些能够序列化任何动态数组的包装器,甚至包含嵌套记录或动态数组.

Here is some wrapper able to serialize any dynamic array, even containing nested records or dynamic arrays.

4.使用数据库引擎

也许更好的方法是不要让您的数据停留在非进化的二进制形式中,这是您的应用程序专有的.如果要添加属性,则必须手动管理它.或者,如果您想从其他应用程序访问您的数据,这可能会很困难.

Perhaps the better approach is not to have your data stuck in a non-evolving binary form, proprietary to your application. If you want to add a property, you'll have to manage it by hand. Or if you want to access your data from other applications, it may be difficult.

周围有很多数据库解决方案——与其使用外部数据库(如 MS SQL、FireBird 或 Oracle),不如将数据库嵌入到您的应用程序中(更容易安装).值得一提的是 SQLite 它有 很多包装器,包括 我们的版本(如果您想改用 MS SQL 或 Oracle,这将允许您更改到任何其他数据库).

There are a lot of database solutions around - instead of using an external database (like MS SQL, FireBird or Oracle), it could be a good idea to embed the database inside your application (much easier to install). Worth mentioning SQLite which has a lot of wrappers, including our version (which will allow you to change to any other database if you want to use MS SQL or Oracle instead).

您还有其他解决方案 - 请参阅 this SO question - 如果您需要性能,请查看我们的 大表库.

You have other solutions around - see this SO question - and if you need performance, take a look at our Big Table library.

这篇关于Delphi:以某种结构存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆