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

查看:48
本文介绍了链接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天全站免登陆