Recent

Author Topic: removing parts of TStringList  (Read 3207 times)

mrkaban

  • Jr. Member
  • **
  • Posts: 60
    • КонтинентСвободы
removing parts of TStringList
« on: January 12, 2017, 02:53:11 pm »
Hello dear friends! Thank you for regular care!

I want to apologize for my English.

I want to remove some of the TStringList! Remove not the whole line, and the part from a certain combination of symbols. For example, starting with a combination of "ver" or "1.2".

Currently quotes are removed from the TStringList with StringReplace, but it only replaces. Here's what we have now:

Code: Pascal  [Select][+][-]
  1.   MyList2.Text  := StringReplace(MyList2.Text, '"', '', [rfReplaceAll, rfIgnoreCase]);
  2.        with MyList2.Create do
  3.   try
  4.     MyList2.StrictDelimiter := true;
  5.  
  6.     s := '';
  7.     for i := 0 to MyList2.Count - 1 do if Trim(MyList2.Strings[i]) <> '' then
  8.       s := s + Format('%s(name LIKE "%s")', [IfThen(i = 0, '', ' OR '), MyList2.Strings[i]]);
  9.     s := 'SELECT * FROM program WHERE ' + s + ' ORDER BY id';
  10.   finally
  11.   //  Free;
  12.   end;                  
  13.  
  14.  

I understand that you want to use:

Code: Pascal  [Select][+][-]
  1. UTF8Delete(FiltrStr,Pos('ver',FiltrStr),Length(FiltrStr));

But I can not figure out how to do it so that the SQL statement formed correctly.

Thanks in advance for any help!


Special thanks to the developers for a good development environment with open source!
« Last Edit: January 12, 2017, 02:55:16 pm by mrkaban »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: removing parts of TStringList
« Reply #1 on: January 12, 2017, 03:21:36 pm »
I think you need more code to parse the data correctly. A one liner replace command is not enough.
SQL or any language syntax is not trivial.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: removing parts of TStringList
« Reply #2 on: January 12, 2017, 03:24:52 pm »
Maybe you can post some input and desired output we can see.

mrkaban

  • Jr. Member
  • **
  • Posts: 60
    • КонтинентСвободы
Re: removing parts of TStringList
« Reply #3 on: January 12, 2017, 03:29:15 pm »
Yes. of course. Thank you!

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TfMian.mBeginSerchClick(Sender: TObject);
  3. var
  4.    MyList: TStringListUTF8;     // To store the registry key names
  5.    MyRegistry: TRegistry;      
  6.      s: string;
  7.      i : integer;
  8.  //     p: Integer;   // MyList2 declared as a global variable
  9.    Str: string;
  10.      N:integer;        // Write in the list of base
  11. begin
  12. Cursor:= crHourGlass;
  13.  bSearch.Visible:=False;   // Hide the button to start search
  14.       // Clear the old contents
  15.         SQLQuery1.Close;                               // Table in the purification, in which
  16.         SQLQuery1.SQL.Text := 'delete from install';    // Recorded programs list
  17.         SQLQuery1.ExecSQL;
  18.         SQLTransaction1.CommitRetaining;
  19.  
  20.   MyList2:=TStringListUTF8.Create;
  21.  MyRegistry:=TRegistry.Create(KEY_WOW64_64KEY);
  22.  MyList:=TStringListUTF8.Create;
  23.  
  24. then begin              
  25.   with MyRegistry do
  26.         begin
  27.         RootKey:=HKEY_LOCAL_MACHINE;
  28.         OpenKeyReadOnly('Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\');
  29.         GetKeyNames(MyList);
  30.         CloseKey;
  31.         for i:=0 to MyList.Count-1 do
  32.            begin
  33.            RootKey:=HKEY_LOCAL_MACHINE;
  34.            OpenKeyReadOnly('Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\'+
  35.            MyList[i]);
  36.            Str:=ReadString('DisplayName');
  37.            if Str<>'' then
  38.            MyList2.Add(CP1251ToUTF8(ReadString('DisplayName')));
  39.            CloseKey;
  40.            end;
  41.  
  42.         RootKey:=HKEY_LOCAL_MACHINE;
  43.         OpenKeyReadOnly('Software\Microsoft\Windows\CurrentVersion\Uninstall\');
  44.         GetKeyNames(MyList);
  45.         CloseKey;
  46.         for i:=0 to MyList.Count-1 do
  47.            begin
  48.            RootKey:=HKEY_LOCAL_MACHINE;
  49.            OpenKeyReadOnly('Software\Microsoft\Windows\CurrentVersion\Uninstall\'+
  50.            MyList[i]);
  51.            Str:=ReadString('DisplayName');
  52.            if Str<>'' then
  53.            MyList2.Add(CP1251ToUTF8(ReadString('DisplayName')));
  54.            CloseKey;
  55.            end;
  56.           end;
  57.  
  58.  
  59.   SQLQuery1.Close;
  60.   SQLQuery1.SQL.Clear;
  61.   SQLQuery1.SQL.Add('insert into install(text)'); // Write the resulting list to the database program
  62.   SQLQuery1.SQL.Add('Values (:pText)');
  63.   for N := 0 to MyList2.Count - 1 do
  64.   begin
  65.     SQLQuery1.ParamByName('pText').AsString := MyList2[N];
  66.     SQLQuery1.ExecSQL;
  67.   end;
  68.   SQLTransaction1.CommitRetaining;
  69.  
  70.    //новый вариант
  71.   SQLQuery1.SQL.Clear;
  72. SQLQuery1.SQL.Text:='select text from install';
  73. SQLQuery1.Open;
  74. SQLQuery1.First;
  75. MyList2.clear;                   // I tried from the database to obtain a list of programs
  76. while not SQLQuery1.Eof do
  77. begin
  78.   MyList2.Add(SQLQuery1.FieldByName('text').AsString);
  79.   SQLQuery1.Next;
  80. end;
  81. SQLQuery1.Close;
  82.  
  83.  
  84.          //Try removing quotes
  85.   MyList2.Text  := StringReplace(MyList2.Text, '"', '', [rfReplaceAll, rfIgnoreCase]);
  86.        with MyList2.Create do
  87.   try
  88.     MyList2.StrictDelimiter := true;
  89.  //   UTF8Delete(FiltrStr,Pos('ver',FiltrStr),Length(FiltrStr));
  90.     s := '';
  91.     for i := 0 to MyList2.Count - 1 do if Trim(MyList2.Strings[i]) <> '' then
  92.       s := s + Format('%s(name LIKE "%s")', [IfThen(i = 0, '', ' OR '), MyList2.Strings[i]]);
  93.     s := 'SELECT * FROM program WHERE ' + s + ' ORDER BY id';
  94.   finally
  95.   //  Free;  // I do not release, because it is used in other procedures
  96.   end;
  97.  
  98.   // everything is stored in the s request, and it is used to retrieve data from the database
  99. //  Showmessage(s);
  100.  
  101.   MyList.Free;
  102. SQLQuery1.Close;
  103. SQLQuery1.Active:=false;
  104. SQLQuery1.SQL.Clear;
  105. SQLQuery1.SQL.Add(s);
  106. SQLQuery1.Active:=true;
  107. SQLQuery1.Open;
  108.  
  109.             // Fill the column headings and changing the column width
  110. DBGrid1.Columns[1].Title.Caption:='Название';
  111. DBGrid1.Columns[2].Title.Caption:='Тип ПО';
  112. DBGrid1.Columns[3].Title.Caption:='Лицензия';
  113. DBGrid1.Columns[4].Title.Caption:='Стоимость';
  114. DBGrid1.Columns[5].Title.Caption:='Замена';
  115. DBGrid1.Columns[1].Width:= 200;
  116. DBGrid1.Columns[2].Width:= 150;
  117. DBGrid1.Columns[3].Width:= 110;
  118. DBGrid1.Columns[4].Width:= 90;
  119. DBGrid1.Columns[5].Width:= 150;
  120.  
  121. end;                                  
  122.  

 

TinyPortal © 2005-2018