如何查询 TFDMemTable

How to query a TFDMemTable

我正在尝试创建一个简单的循环来处理 TFDMemTable。我在表单上放了一个 TFDMemTable、一个 TFDQuery 和一个 TFDConnection,我想我已经正确连接了所有内容,但是当我激活查询时它返回一个错误消息指出不存在 table 作为 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

要查询 FDMemTable,您可以使用 FireDAC 的 LocalSQL 工具。添加一个 TFDLocaSQL 组件到你的表单可以让你注册一个或多个 TDataSet-descendant 数据集(使用它的 Datasets 属性 的 Add 方法),比如你的 FDMemTable , 成为使用 Sqlite 的 SQL 实现的 SQL 搜索的目标。它应该足以执行您的查询并且非常易于使用,一旦您设置了它。

有关详细信息,请参阅 https://docwiki.embarcadero.com/RADStudio/Sydney/en/Local_SQL_(FireDAC)

这是我的一个答案中的代码,它使用带有 FDMemTable 的 FireDAC localSQL。代码是用 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;

实际答案如 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();
    }
}