【问题标题】:Create table with firedac without SQL Script使用没有 SQL 脚本的 fireac 创建表
【发布时间】:2017-05-15 04:19:08
【问题描述】:

Firedac 库集中了数据库行为,并且有很多方法可以正常工作而无需关心数据库类型。实际上,Firedac 使用大多数常见数据库的本地驱动程序,隐藏了语法上的细微差异,允许非常灵活地更改数据库平台。
例如,生成器和 autoinc 字段很容易检测到,CAST 和参数工作正常,允许在数据库之间轻松迁移。

如何在不实例化 FDQuery 的情况下使用 Firedac 功能创建新表,它运行 SQL 脚本 CREATE TABLE

我希望创建任何对象,并为每个对象字段调用特定的 FieldByName,将其记录在数据库中,但首先我需要证明:

  1. 如果表已经创建
  2. 如果字段已创建
  3. 如果记录已创建

这是我目前拥有的代码:

TRecCustomer = record
  Id:integer;
  Name:String;
  Birthday:TDate;
end;

ICustomer = interface
  procedure setCustomerId(Value: Integer);
  procedure setCustomerName(Value: String);
  procedure SetBirthday(Value: TDate);
  procedure Post;
end;

TCustomer = class(TInterfacedObjet, ICustomer)
  CustomerObject=TRecCustomer;

  procedure setCustomerId(Value: Integer);
  procedure setCustomerName(Value: String);
  procedure SetBirthday(Value: TDate);
  procedure Post;
end;

procedure TCustomer.Post;
begin
  if not TableExists('Customer') then CreateTable('Customer');
  if not FieldExists('Name') then CreateField('Customer','name',ftString,[],40);
  if not FieldExists('Id') then CreateField('Customer','Id',ftInteger,[cAutoInc,cNotNull]);
  if not FieldExists('Birthday') then CreateField('Customer','birthday',ftDate);
end;

想象一下过程

CreateTable(Tablename: String)
CreateField(FieldName: String; FieldType: TDataType; Constraints: TConstraints; Length: Integer = 0);

在哪里

TConstraints = set of (cAutoInc, cNotNull, cUnique, cEtc);

我可以为特定数据库执行此操作,例如 Sqlite 或 Firebird,但我不知道如何为使用 Firedac 资源的任何数据库执行此操作。


我找到了@Ondrej Kelle 建议的FireDAC.Comp.Client.TFDTable.CreateTable(ARecreate: Boolean = True; AParts: TFDPhysCreateTableParts = [tpTable .. tpIndexes]),但我不明白AParts 的用法。有人有例子吗? http://docwiki.embarcadero.com/Libraries/Berlin/en/FireDAC.Comp.Client.TFDTable.CreateTable

非常感谢。

【问题讨论】:

  • 感谢@OndrejKelle,我根据你的建议编辑了这个问题,但我还需要一个例子来做到这一点。

标签: database delphi design-patterns firedac


【解决方案1】:

您可以创建一个TFDTable 对象,至少通过指定TableName 并在FieldDefs 集合中添加字段定义来描述您的表。在实践中,您通常还会为某些字段创建主键索引(这可以通过 AddIndex 方法完成)。在您描述您的表格后,调用CreateTable 方法。一个最小的例子可以是:

var
  Table: TFDTable;
begin
  Table := TFDTable.Create(nil);
  try
    Table.Connection := FDConnection1;
    { specify table name }
    Table.TableName := 'MyTable';
    { add some fields }
    Table.FieldDefs.Add('ID', ftInteger, 0, False);
    Table.FieldDefs.Add('Name', ftString, 50, False);
    { define primary key index }
    Table.AddIndex('pkMyTableID', 'ID', '', [soPrimary]);
    { and create it; when the first parameter is True, an existing one is dropped }
    Table.CreateTable(False);
  finally
    Table.Free;
  end;
end;

【讨论】:

  • 如果你想使用SQLite ROWID feature(ID列INTEGER PRIMARY KEY也是行标识符,因此会自动生成),将字段ID的类型从ftInteger更改为ftAutoIncrement,并将Required参数为真,即Table.FieldDefs.Add('ID', ftAutoIncrement, 0, True);。 FireDAC 知道该怎么做。然后约定也称为字段 ROWID。事实上,在测试中,即使没有创建索引,这似乎也可以(使用 FireDAC)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 2011-08-11
  • 2011-12-13
  • 2012-08-15
  • 1970-01-01
相关资源
最近更新 更多