【问题标题】:How to query a TFDMemTable如何查询 TFDMemTable
【发布时间】:2021-09-15 16:39:09
【问题描述】:

我正在尝试创建一个简单的循环来处理TFDMemTable。我在表单上删除了TFDMemTableTFDQueryTFDConnection,我认为我已经正确连接了所有内容,但是当我激活查询时,它会返回一条错误消息,指出没有这样的表为 FDMemTable1。

这是我正在尝试的演示代码:

#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include "FireDac.Stan.Def.hpp"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
void __fastcall TForm2::FormCreate(TObject *Sender)
{
    FDMemTable1->InsertRecord(ARRAYOFCONST((L"Vic", 100, 30)));
    FDMemTable1->InsertRecord(ARRAYOFCONST((L"Jeong", 20, 20)));
    FDMemTable1->InsertRecord(ARRAYOFCONST((L"Christina", 400, 50)));
    FDMemTable1->InsertRecord(ARRAYOFCONST((L"Justina", 0, 60)));

    FDConnection1->ConnectionName = L"FDMemTable1";
    FDConnection1->DriverName = L"SQLite";
    FDConnection1->Connected = true;
    FDQuery1->Connection = FDConnection1;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
    UnicodeString x;
    FDQuery1->SQL->Text = "SELECT * FROM FDMemTable1";
    FDQuery1->Active = true;

    if (!FDQuery1->Eof) {
        x = FDQuery1->FieldByName(L"FullName")->AsString;
        
        ...

        FDQuery1->Next();
    }
}

如何查询现有的TFDMemTable

【问题讨论】:

  • 您看过 Embarcadero 的 TFDMemTable 示例了吗?

标签: c++builder firedac


【解决方案1】:

要查询 FDMemTable,您可以使用 FireDAC 的 LocalSQL 工具。向表单添加 TFDLocaSQL 组件使您能够注册一个或多个 TDataSet 后代数据集(使用其 Datasets 属性的 Add 方法),例如您的 FDMemTable,作为使用 Sqlite 的 SQL 实现的 SQL 搜索的目标.它应该足以执行您的查询并且非常易于使用,一旦您进行了设置。

更多信息请参见https://docwiki.embarcadero.com/RADStudio/Sydney/en/Local_SQL_(FireDAC)

这是我的答案中的代码,它使用 FireDAC 的 localSQL 和 FDMemTable。代码是用 Dellphi 编写的,但翻译成 c++ 应该很简单:

  FDConnection1.DriverName := 'SQLite';
  FDConnection1.Connected := True;

  FDLocalSQL1.Connection := FDConnection1;
  FDLocalSQL1.DataSets.Add(FDMemTable1);

  FDLocalSQL1.Active := True;

  FDQuery1.SQL.Text := 'select * from FDMemTable1 order by ID limit 5';
  FDQuery1.Active := True;

【讨论】:

    【解决方案2】:

    事实证明,正如 MartynA 所建议的那样。这是最终的有效代码:

    #include <vcl.h>
    #pragma hdrstop
    #include "Unit2.h"
    #include "FireDac.Stan.Def.hpp"
    //---------------------------------------------------------------------------
    
    #pragma package(smart_init)
    #pragma link "AdvGrid"
    #pragma link "AdvObj"
    #pragma link "AdvUtil"
    #pragma link "BaseGrid"
    #pragma link "DBAdvGrid"
    #pragma resource "*.dfm"
    TForm2 *Form2;
    //---------------------------------------------------------------------------
    void __fastcall TForm2::FormCreate(TObject *Sender)
    {
        FDMemTable1->Active = true;
        FDMemTable1->InsertRecord(ARRAYOFCONST((L"Vic", 100, 30)));
        FDMemTable1->InsertRecord(ARRAYOFCONST((L"Jeong", 20, 20)));
        FDMemTable1->InsertRecord(ARRAYOFCONST((L"Christina, 400, 50)));
        FDMemTable1->InsertRecord(ARRAYOFCONST((L"Justina", 0, 60)));
    
        FDConnection1->ConnectionName = L"FDMemTable1";
        FDConnection1->DriverName = L"SQLite";
        FDConnection1->LoginPrompt = false;
        FDConnection1->Connected = true;
    
        FDLocalSQL1->Connection = FDConnection1;
        FDLocalSQL1->DataSets->Add(FDMemTable1, L"FDMemTable1");
        FDLocalSQL1->Active = true;
    
        FDQuery1->Connection = FDConnection1;
    }
    
    //---------------------------------------------------------------------------
    void __fastcall TForm2::Button1Click(TObject *Sender)
    {
        UnicodeString x;
        FDQuery1->SQL->Text = "SELECT * FROM FDMemTable1";
        FDQuery1->Active = true;
    
        if (!FDQuery1->Eof) {
            x = FDQuery1->FieldByName(L"FullName")->AsString;
    
            FDQuery1->Next();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 2020-04-19
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多