使用不带SQL脚本的firedac创建表 [英] Create table with firedac without SQL Script

查看:192
本文介绍了使用不带SQL脚本的firedac创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firedac库集中了数据库行为,并且有很多方法可以很好地工作而无需关心数据库类型。实际上,Firedac对大多数常用数据库使用本机驱动程序,在语法上隐藏了细微的差别,从而可以非常灵活地更改数据库平台。

例如,生成器和autoinc字段很容易检测到,CAST和参数工作正常,从而易于迁移

Firedac library centralizes database behavior and have a lot of methods which works fine without care about the Database Type. Actually, using native drivers for most common databases, Firedac hides subtle differences on syntax allowing very flexible changes of database platform.
For example, generators and autoinc fields are easily detectable, CAST and parameters works fine allowing easy migration between databases.

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

How to use Firedac power to create New Table without instantiate FDQuery, which runs a SQL Script CREATE TABLE?

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

I hope to create any Object and, calling specific FieldByName for each Object Field, record it on database, but first I need to certify:


  1. 如果已经创建表

  2. 如果已经创建字段

  3. 如果已经创建记录

到目前为止,这是我拥有的代码:

This is the code I have, so far:

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)做到这一点,但我不知道

I can do it for specific database, for example Sqlite or Firebird, but I don't know hou to do for any database using Firedac resources.

我发现 FireDAC.Comp.Client.TFDTable .CreateTable(ARecreate:Boolean = True; AParts:TFDPhysCreateTableParts = [tpTable .. tpIndexes]),由@Ondrej Kelle建议,但我不理解 AParts 用法。有人有例子吗? http://docwiki.embarcadero.com/Libraries/柏林/zh-CN/FireDAC.Comp.Client.TFDTable.CreateTable

I found FireDAC.Comp.Client.TFDTable.CreateTable(ARecreate: Boolean = True; AParts: TFDPhysCreateTableParts = [tpTable .. tpIndexes]), suggested by @Ondrej Kelle but I don't understood AParts usage. Somebody have an example? http://docwiki.embarcadero.com/Libraries/Berlin/en/FireDAC.Comp.Client.TFDTable.CreateTable

非常感谢。

推荐答案

您可以创建> TFDTable 对象,至少通过指定 TableName 并在 FieldDefs 集合。实际上,您通常还会为某些字段创建主键索引(这可以通过 AddIndex 方法)。描述表之后,调用 CreateTable 方法。最小的示例可以是:

You can create a TFDTable object, describe your table at least by specifying TableName and adding field definitions in the FieldDefs collection. In practice you'll usually create also a primary key index for some field (that's what can be done by the AddIndex method). After you describe your table, call CreateTable method. A minimal example can be:

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;

这篇关于使用不带SQL脚本的firedac创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆