?
Solved

A component to keep string

Posted on 2006-11-22
6
Medium Priority
?
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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