连接4条信息并保存 [英] linking 4 pieces of information and saving them

查看:111
本文介绍了连接4条信息并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保存,编辑和加载信息。我要加载的信息是我会添加的。每条信息将包含4个(字符串,整数,字符串,整数)。通过4个单独的编辑框和一个按钮,我将这个信息添加到一个数据库(不知道我需要一个数据库,或者如果它可以通过类似Tstringlist的东西来完成)。每次点击按钮时,它将添加在数据库中的那一刻输入的内容。

Saving, editing and loading information. The information that I want to load is something I will add myself. Each line of information will contain 4 pieces, (string, integer, string, integer). Via 4 seperate edit boxes and a button I will add this information to a 'database' (not sure if I need a database or if it can be done via something like a Tstringlist). Everytime the button is clicked it will added the content that is typed at that moment in the 'database'.

保存数据的唯一需求是当我输入第一个从列表中可以将属于它的其余信息放在一个备忘录或编辑框中。所以我想我必须能够搜索。只是想保持尽可能简单。只有大约10到15行信息。如果可能的话,我可以稍后再次加载它们。

The only demand of the saved data is when I type the first string from the list it could place the rest of the information that belongs to it in a memobox or edit boxes as well. So I suppose I have to be able to search. Just want to keep it as simple as possible. There will only be about 10 to 15 lines of information. and if possible it would be good if I can load them again a later time.

推荐答案

这是一些非常基本的代码,应该得到你在路上没有错误检查,您无疑将要开发并进一步修改。关键是应该有一些想法来帮助您编写适合您的代码。

Here's some very basic code that should get you on your way. There's no error checking, and you'll no doubt want to develop it and modify it further. The point is that there should be some ideas to help you write code that works for you.

现在我已经以逗号分隔字段,但没有尝试处理出现任何值的逗号。如果这是一个问题,请选择一个不同的分隔符,或者转义逗号。我已经玩弄自己的每一个字段(有效地使用换行符作为分隔符),但这会使阅读代码更加棘手。

Now that I have comma-separated the fields, but made no attempt to handle the appearance of commas in any of the values. If this is a problem then choose a different delimiter, or escape the commas. I had toyed with writing each field on its own line (effectively using a newline as the separator), but this makes the reading code more tricky to write.

再次,要点是这不是最终的生产代码,而是要给你一个起点。

Again, the main point is that this is not final production code, but is intended to give you a starting point.

function Split(const s: string; Separator: char): TStringDynArray;
var
  i, ItemIndex: Integer;
  len: Integer;
  SeparatorCount: Integer;
  Start: Integer;
begin
  len := Length(s);
  if len=0 then begin
    Result := nil;
    exit;
  end;

  SeparatorCount := 0;
  for i := 1 to len do begin
    if s[i]=Separator then begin
      inc(SeparatorCount);
    end;
  end;

  SetLength(Result, SeparatorCount+1);
  ItemIndex := 0;
  Start := 1;
  for i := 1 to len do begin
    if s[i]=Separator then begin
      Result[ItemIndex] := Copy(s, Start, i-Start);
      inc(ItemIndex);
      Start := i+1;
    end;
  end;
  Result[ItemIndex] := Copy(s, Start, len-Start+1);
end;

type
  TValue = record
    i1, i2: Integer;
    s: string;
  end;

  TMyDict = class(TDictionary<string,TValue>)
  public
    procedure SaveToFile(const FileName: string);
    procedure LoadFromFile(const FileName: string);
  end;

{ TMyDict }

procedure TMyDict.SaveToFile(const FileName: string);
var
  Strings: TStringList;
  Item: TPair<string,TValue>;
begin
  Strings := TStringList.Create;
  Try
    for Item in Self do begin
      Strings.Add(Format(
        '%s,%s,%d,%d',
        [Item.Key, Item.Value.s, Item.Value.i1, Item.Value.i2]
      ));
    end;
    Strings.SaveToFile(FileName);
  Finally
    FreeAndNil(Strings);
  End;
end;

procedure TMyDict.LoadFromFile(const FileName: string);
var
  Strings: TStringList;
  Item: TPair<string,TValue>;
  Line: string;
  Fields: TStringDynArray;
begin
  Strings := TStringList.Create;
  Try
    Strings.LoadFromFile(FileName);
    for Line in Strings do begin
      Fields := Split(Line, ',');
      Assert(Length(Fields)=4);
      Item.Key := Fields[0];
      Item.Value.s := Fields[1];
      Item.Value.i1 := StrToInt(Fields[2]);
      Item.Value.i2 := StrToInt(Fields[3]);
      Add(Item.Key, Item.Value);
    end;
  Finally
    FreeAndNil(Strings);
  End;
end;

请注意,您不要尝试在磁盘上搜索文件。你只需将它加载到内存中,进入字典,然后从中查找。

Note that you don't attempt to search the file on disk. You simply load it into memory, into the dictionary and look things up from there.

当您始终使用相同的键时,字典很棒。如果你有多个键,那么字典不太方便,但是如果你只有15条记录,那么关心性能的影响呢?!

A dictionary is great when you always use the same key. If you have multiple keys then a dictionary is less convenient, but who cares about the performance impact if you've only got 15 records?!

免责声明:我没有运行代码,我没有测试过,等等。

Disclaimer: I've not run the code, I've not tested it, etc. etc.

这篇关于连接4条信息并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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