2017-05-15 1 views
3

Firedac 라이브러리는 데이터베이스 동작을 중앙 집중화하고 데이터베이스 유형에 상관없이 잘 작동하는 많은 메소드를 가지고 있습니다. 실제로 Firedac은 가장 일반적인 데이터베이스에 기본 드라이버를 사용하여 신택스에 미묘한 차이점을 숨겨 데이터베이스 플랫폼의 매우 유연한 변경을 허용합니다.
예를 들어, 생성자 및 autoinc 필드를 쉽게 감지 할 수 있으며 CAST 및 매개 변수가 올바르게 작동하여 데이터베이스간에 쉽게 마이그레이션 할 수 있습니다.SQL 스크립트없이 firedac을 사용하여 테이블 생성

Firedac Power를 사용하여 SQL 스크립트 CREATE TABLE을 실행하는 FDQuery를 인스턴스화하지 않고 새 테이블을 만드는 방법은 무엇입니까?

나는 어떤 객체를 만들려면 희망, 각 개체 필드에 대한 특정 FieldByName를 호출 데이터베이스에 기록하지만, 처음 내가 증명해야합니다

  1. 표가 이미
  2. 을 만든 경우 필드 인 경우

    : 레코드가 이미 지금까지

이것은 내가 가지고있는 코드를 작성하는 경우 이미

  • 을 생성
    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; 
    

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

    내가 예를 SQLite는 또는 파이어 버드를 들어, 특정 데이터베이스를 위해 그것을 할 수있는 절차

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

    상상,하지만 난 허우가 사용하는 데이터베이스에 대한 해야할지 모르겠어요 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

    고마워요.

  • +3

    시도 ['TFDTable.CreateTable' (http://docwiki.embarcadero.com/Libraries/Berlin/en/FireDAC.Comp.Client.TFDTable.CreateTable) –

    +0

    감사 @ OndrejKelle, 나는 당신의 권고와 함께 질문을 편집했으나 아직 그 예를 들어야합니다. –

    답변

    2

    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;