* * *

Author Topic: How to make a table  (Read 327 times)

Thomas Colvin

  • New member
  • *
  • Posts: 11
How to make a table
« on: September 17, 2017, 04:29:37 pm »
In Borland Delphi 3 have Database Wizard and can make tables.

How do I make a table in Lazarus?

Thanks, Thomas

Handoko

  • Hero Member
  • *****
  • Posts: 1802
  • My goal: build my own game engine using Lazarus
Re: How to make a table
« Reply #1 on: September 17, 2017, 04:58:46 pm »
You can search the web for free database tools. For dbf database, you can try CDBF Explorer 0.60 or MyDbf Studio 0.4 Beta. Both are free, I use them.

http://www.softpedia.com/get/Internet/Servers/Database-Utils/CDBF-Explorer.shtml
http://mydbfstudio.altervista.org/

I prefer to generate the table files manually if my program can't find them when starting. Here is my code for generating dbf tables:

Code: Pascal  [Select]
  1. procedure DataFilePrepare;
  2. var
  3.   NewFieldDefs: TDbfFieldDefs;
  4.   NewFieldDef: TDbfFieldDef;
  5.   isRestructureNeeded: Boolean;
  6.   Z: Integer;
  7. begin
  8.   with frmMain.Database, NewFieldDef do
  9.   begin
  10.  
  11.     // Create table and first field if the table isn't existed
  12.     if (not(FileExists(FilePathFull+DirectorySeparator+TableName))) then
  13.     begin
  14.       FieldDefs.Add('GROUPID', ftInteger, 0, True);
  15.       CreateTable;
  16.     end;
  17.  
  18.     // Create all remain fields if they're not existed
  19.     Open;
  20.     isRestructureNeeded := False;
  21.     NewFieldDefs := TDbfFieldDefs.Create(frmMain);
  22.     NewFieldDefs.Assign(DbfFieldDefs);
  23.     Z := FieldIndex('SORTID');
  24.     if (Z < 0) then
  25.     begin
  26.       NewFieldDef := NewFieldDefs.AddFieldDef;
  27.       FieldName := 'SORTID';
  28.       FieldType := ftInteger;
  29.       Required := True;
  30.       isRestructureNeeded := True;
  31.     end;
  32.     Z := FieldIndex('DATE');
  33.     if (Z < 0) then
  34.     begin
  35.       NewFieldDef := NewFieldDefs.AddFieldDef;
  36.       FieldName := 'DATE';
  37.       FieldType := ftDate;
  38.       Required := False;
  39.       isRestructureNeeded := True;
  40.     end;
  41.     Z := FieldIndex('TEXT');
  42.     if (Z < 0) then
  43.     begin
  44.       NewFieldDef := NewFieldDefs.AddFieldDef;
  45.       FieldName := 'TEXT';
  46.       FieldType := ftString;
  47.       Size := 60;
  48.       Required := False;
  49.       isRestructureNeeded := True;
  50.     end;
  51.     Z := FieldIndex('MEMO');
  52.     if (Z < 0) then
  53.     begin
  54.       NewFieldDef := NewFieldDefs.AddFieldDef;
  55.       FieldName := 'MEMO';
  56.       FieldType := ftMemo;
  57.       Required := False;
  58.       isRestructureNeeded := True;
  59.     end;
  60.     Close;
  61.  
  62.     // Restructure the table if new field is added
  63.     if (isRestructureNeeded) then
  64.     begin
  65.       RestructureTable(NewFieldDefs, True);
  66.       DeleteFile((FilePathFull+DirectorySeparator+DataFileName+'_bck.dbf'));
  67.     end;
  68.     NewFieldDefs.Free;
  69.  
  70.   end;
  71. end;
« Last Edit: September 17, 2017, 05:00:29 pm by Handoko »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus