我正在尝试创建一个简单的循环来处理TFDMemTable。我已经在表单上放置了一个TFDMemTable、一个TFDQuery和一个TFDConnection,我想我已经正确地连接了所有内容,但是当我激活查询时,它会返回一条错误消息,指出没有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
发布于 2021-09-15 17:38:25
要查询FDMemTable,您可以使用FireDAC的LocalSQL工具。将TFDLocaSQL组件添加到窗体使您能够注册一个或多个TDataSet派生数据集(使用其Datasets属性的Add方法),例如您的FDMemTable,以使用Sqlite实现将其注册为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;发布于 2021-09-16 04:55:37
实际的答案是如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();
}
}https://stackoverflow.com/questions/69196896
复制相似问题