A component to keep string

Hello guys,

I would like to know if there is a component to keep string.

My system will create an access database and all tables I have in script code.

Today they are in my .pas but I would like to keep the code in a component as a

container for string.

any suggestion?
LVL 1
hidrauAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TheRealLokiConnect With a Mentor Senior DeveloperCommented:
here's a demo i made for you on how to keep a list of database table creation strings (sqls) and find them by name if need be. you can Load and Save the list to file.

unit DatabaseStructureObject;

interface

uses windows, classes, sysutils, contnrs; // required for TObjectList;

type
  TTableInfo = class(TObject)
  public
    Strings: TStrings;
    Name: string;
    constructor Create;
    destructor Destroy; override;
  end;

type TDatabaseStructureObject = class(TObject)
  private
    function GetTable(index: integer): TTableInfo;
    function GetTableByName(s: string): TTableInfo;
  public
    TableList: TObjectList;
    constructor Create;
    destructor Destroy; override;
    procedure LoadFromFile(filename: string);
    procedure SaveToFile(filename: string);
    property Table[index: integer]: TTableInfo read GetTable;
    property TableByName[s: string]: TTableInfo read GetTableByName;
    procedure AddTableInfo(tablename: string; SQLText: TStrings);
    function IndexOfTableName(tablename: string): integer;
  end;

implementation

{ TTableInfo }

constructor TTableInfo.Create;
begin
  inherited;
  Strings := TStringList.Create;
end;

destructor TTableInfo.Destroy;
begin
  Strings.Clear;
  Strings.Free;
  inherited;
end;

{ TDatabaseStructureObject }

constructor TDatabaseStructureObject.Create;
begin
  inherited;
  TableList := TObjectList.Create;
end;

destructor TDatabaseStructureObject.Destroy;
begin
  TableList.Clear;
  TableList.Free;
  inherited;
end;

function TDatabaseStructureObject.GetTable(index: integer): TTableInfo;
begin
  result := TTableInfo(TableList[index]);
end;

procedure TDatabaseStructureObject.SaveToFile(filename: string);
var
  FS: TFileStream;
  i, j, cnt, linecnt, linesz: integer;
  s: string;
begin
  FS := TFileStream.Create(filename, fmCreate);
  try
    cnt := TableList.Count; // get the number of tables we wish to store in the file
    FS.Write(cnt, sizeof(cnt));
    for i := 0 to pred(cnt) do
    begin // loop and write each table structure to the file
      linesz := length(Table[i].Name);
      FS.Write(linesz, sizeof(linesz)); // write how long the name string is
      FS.Write(Table[i].Name[1], length(Table[i].Name)); // write the name of one table
      linecnt := Table[i].Strings.Count;
      FS.Write(linecnt, sizeof(linecnt)); // write how many lines are in the table creation sql text
      for j := 0 to pred(Table[i].Strings.Count) do
      begin // loop and write each line of the table creation sql text
        linesz := length(Table[i].Strings[j]);
        FS.Write(linesz, sizeof(linesz)); // write how long the line is
        s := Table[i].Strings[j]; // get 1 line
        FS.Write(S[1], length(S)); // write 1 line
      end;
    end;
  finally
    FS.Free;
  end;
end;

procedure TDatabaseStructureObject.LoadFromFile(filename: string);
var
  FS: TFileStream;
  i, j, k, cnt, linecnt, linesz: integer;
  s: string;
  newTableInfo: TTableInfo;
begin
  TableList.Clear; // empty our current list
  FS := TFileStream.Create(filename, fmOpenRead + fmShareDenyNone);
  try
    FS.Read(cnt, sizeof(cnt)); //get the number of tables stored in the file
    for i := 0 to pred(cnt) do
    begin // loop and read each table structure from the file
      newTableInfo := TTableInfo.Create;
      FS.Read(linesz, sizeof(linesz));
      SetLength(newtableinfo.Name, linesz);
      FS.Read(newtableinfo.Name[1], linesz); // read the name
      FS.Read(linecnt, sizeof(linecnt)); // read how many lines there are in teh table creation sql text
      for j := 0 to pred(linecnt) do
      begin // loop and read each line of the table creation sql text
        FS.Read(linesz, sizeof(linesz)); // read how long this line is
        SetLength(s, linesz);
        FS.Read(s[1], linesz); // read the line
        newtableinfo.Strings.Add(S); // add this line to out table info
      end;
      TableList.Add(newtableinfo); // finally - add this table info to our list
    end;
  finally
    FS.Free;
  end;
end;

procedure TDatabaseStructureObject.AddTableInfo(tablename: string; SQLText: TStrings);
var
  newtableinfo: TTableInfo;
begin
  newtableinfo := TTableInfo.Create;
  newtableinfo.Name := tablename;
  newtableinfo.Strings.Assign(SQLText);
  TableList.Add(newtableinfo);
end;

function TDatabaseStructureObject.GetTableByName(s: string): TTableInfo;
var
  i: integer;
begin
  result := nil;
  i := IndexOfTableName(s);
  if i <> -1 then
    result := Table[i];
end;

function TDatabaseStructureObject.IndexOfTableName(tablename: string): integer;
var
  i: integer;
begin
  result := -1;
  tablename := lowercase(tablename);
  i := 0;
  while ( (result = -1) and (i < TableList.Count) ) do
  begin
    if lowercase(Table[i].Name) = tablename then
      result := i
    else inc(i);
  end;
end;

end.

// ***************************
// DEMO FORM - UNIT1.pas
// ***************************

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DatabaseStructureObject, StdCtrls;

type
  TForm1 = class(TForm)
    bAddtableInfo: TButton;
    bLoad: TButton;
    bSave: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    eTableName: TEdit;
    Memo_SQL: TMemo;
    bShow: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure bAddtableInfoClick(Sender: TObject);
    procedure bLoadClick(Sender: TObject);
    procedure bSaveClick(Sender: TObject);
    procedure bShowClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    DatabaseStructure: TDatabaseStructureObject; // our info on table structures
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  DatabaseStructure := TDatabaseStructureObject.Create;
  OpenDialog1.InitialDir := ExtractFilePath(Paramstr(0));
  SaveDialog1.InitialDir := ExtractFilePath(Paramstr(0));
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DatabaseStructure.Free;
end;

procedure TForm1.bAddtableInfoClick(Sender: TObject);
begin
  DatabaseStructure.AddTableInfo(eTablename.Text, memo_SQL.Lines);
end;

procedure TForm1.bLoadClick(Sender: TObject);
begin
  if openDialog1.Execute then
    DatabaseStructure.LoadFromFile(OpenDialog1.FileName);
end;

procedure TForm1.bSaveClick(Sender: TObject);
begin
  if SaveDialog1.Execute then
    DatabaseStructure.SaveToFile(SaveDialog1.FileName);
end;

procedure TForm1.bShowClick(Sender: TObject);
var
  i, j: integer;
begin
  for i := 0 to pred(DatabaseStructure.TableList.Count) do
  begin
    ShowMessage('Table: ' + DatabaseStructure.Table[i].Name + #13#10 +
      DatabaseStructure.Table[i].Strings.Text);
  end;

(* example of how I see your code working

  for i := 0 to pred(DatabaseStructure.TableList.Count) do
  begin
// EITHER
    Adoquery.Sql.Text := DatabaseStructure.Table[i].Strings.Text
// OR
{
    for j := 0 to pred(DatabaseStructure.Table[i].Strings.Count) do
      Adoquery.Sql.Add(   DatabaseStructure.Table[i].Strings[j]   )
}
    Adoquery.execSql;
  end;

// To find the table structure for 1 table you can use "TableByName[]" eg.
    ShowMessage('Table: ' + DatabaseStructure.TableByName['bob'].Name + #13#10 +
      DatabaseStructure.TableByName['bob'].Strings.Text);


*)

end;

end.

// ***************************
// DEMO FORM - UNIT1.dfm THIS IS THE FORM PART
// ***************************

object Form1: TForm1
  Left = 249
  Top = 107
  Width = 572
  Height = 299
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object bAddtableInfo: TButton
    Left = 56
    Top = 24
    Width = 101
    Height = 25
    Caption = 'bAddtableInfo'
    TabOrder = 0
    OnClick = bAddtableInfoClick
  end
  object bLoad: TButton
    Left = 56
    Top = 60
    Width = 101
    Height = 25
    Caption = 'bLoad'
    TabOrder = 1
    OnClick = bLoadClick
  end
  object bSave: TButton
    Left = 56
    Top = 96
    Width = 101
    Height = 25
    Caption = 'bSave'
    TabOrder = 2
    OnClick = bSaveClick
  end
  object eTableName: TEdit
    Left = 252
    Top = 24
    Width = 121
    Height = 21
    TabOrder = 3
    Text = 'BOB'
  end
  object Memo_SQL: TMemo
    Left = 252
    Top = 56
    Width = 185
    Height = 89
    Lines.Strings = (
      'create table bob...'
      'etc...'
      'some more lines'
      'done')
    TabOrder = 4
  end
  object bShow: TButton
    Left = 56
    Top = 136
    Width = 101
    Height = 25
    Caption = 'bShow'
    TabOrder = 5
    OnClick = bShowClick
  end
  object OpenDialog1: TOpenDialog
    Left = 172
    Top = 64
  end
  object SaveDialog1: TSaveDialog
    Left = 172
    Top = 112
  end
end
0
 
ziolkoCommented:
with TStringList You can easily read/write strings and/or textfiles.

ziolko.
0
 
hidrauAuthor Commented:
I want to do something this way:

I will have a structure by name and strings for each option.

this container will keep all my tables code for creation in my new database access.

I will do something this way

For I := 0 to ListContainer.count do
Begin
  Adoquery.Sql.Add(   ListContainer[i]   )
  Adoquery.execSql;
End;

This way it will create all my tables.

I would like a component name to add my codes in project time.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
ziolkoCommented:
still You can use TStringList if You need more than one stringlist (like one string list = one sql table creation command) ) use TObjectList to hold multiple instances of TStringList.

ziolko.
0
 
hidrauAuthor Commented:
could you give me some examples?
0
 
TheRealLokiSenior DeveloperCommented:
you can use the .Text property of strings also if that's easier

Adoquery.Sql.Add(   ListContainer[i]   )
could just be

Adoquery.Sql.Text := S;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.