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

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

问题描述

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



我没有经验在德尔福和我搜索了很长时间的解决方案,但不明白什么是最好的。我认为我的结构化数据的方式是错误的,但它是简单和有效的。但是,如果有更好的方法存储数据,请说明,但请记住,我需要一些更多的解释,而不仅仅是使用一个xml文件,ict或Ttreeview。 (已阅读,但无法使用它)。



数据是:我做了这个产品,我做的下一个产品是这样的,那么我需要清洁吗? True或false。



数据作为一个类(TObject)存储为Productnumber(整数)和一个List,其中包含可以进行下一步的所有产品。该列表包含另一个类(TObject)与Productnumber(整数)和我需要清理(布尔)。我想将这个结构保存在一个文件中,而不会丢失数据并将其读回到相同的结构。



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



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

  Clean_from = class(TObject)
public
myfromNumber:Integer;
mylist:TList;
发布
构造函数创建;
End

Clean_To = class(TObject)
public
myToNumber:Integer;
清理:布尔;
结束;

构造函数Clean_from.Create;
开始
继承创建;
myList:= Tlist.Create;
结束

对于i = 0到100 do
begin
From:= Clean_from.create;
for j:= 0 to 10 do
begin
To:= Clean_To.create;
To.clean:= true或false;
From.myList.add(To);
结束
GlobalList.add(from);
结束

现在我想保存全局列表中的所有内容,所以我可以加载它相同

解决方案

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



1。标准方式



1.1 SaveToStream



在Delphi中,通常实现一个 SaveToStream 方法,它将保存目标中每个对象的内容 TStream (一个 TFileStream TMemoryStream )。



你必须写



1.2类似DFM的流式



请参阅 TWriter / TReader 课程。



如果您在发布的属性中定义数据,则可以使用标准的Delphi类序列化它们。



对于某些可以序列化任何 TCollection的方法往返于 JSON 内容,请参阅这个博客文章



2。 RTTI



参见这个SO问题



特别是,新的增强型RTTI(自Delphi 2010起可用)打开新的机会序列化。



3。使用记录而不是类



如果每个项目都不存储大量内容(某些整数/布尔值),那么使用记录而不是对象对于速度和内存消耗/分片,可能值得。



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



4。使用数据库引擎



也许更好的方法是不要让您的数据以不发展的二进制形式存在于应用程序中。如果要添加属性,则必须手动进行管理。或者如果您想从其他应用程序访问数据,可能会很困难。



有很多数据库解决方案 - 而不是使用外部数据库(如MS SQL,FireBird或Oracle),将数据库嵌入应用程序(安装更简单)可能是一个好主意。值得一提的是 SQLite ,它具有很多包装,包括我们的版本(如果要使用MS SQL或Oracle,则可以更改为任何其他数据库)。



您还有其他解决方案 - 请参阅这个SO问题 - 如果您需要执行性能,请查看我们的大表格库


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 Delphi, 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.

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.

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. The standard way

1.1 SaveToStream

In Delphi, 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-like streaming

See TWriter / TReader classes.

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

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

2. The RTTI

See for instance this SO question.

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

3. Use records instead of classes

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. Use a database engine

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.

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).

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天全站免登陆