Solved

A component to keep string

Posted on 2006-11-22
6
228 Views
Last Modified: 2011-09-20
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?
0
Comment
Question by:hidrau
  • 2
  • 2
  • 2
6 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 17997544
with TStringList You can easily read/write strings and/or textfiles.

ziolko.
0
 
LVL 1

Author Comment

by:hidrau
ID: 17997659
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
 
LVL 21

Expert Comment

by:ziolko
ID: 17997792
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:hidrau
ID: 17998079
could you give me some examples?
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18000445
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
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 total points
ID: 18000450
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now